Python, LXML, and setting xml:lang

Having trouble figuring out how to script the @xml:lang attribute?

For a DITA document that contains a single language, the highest level element (i.e. map, concept, task, etc.) that contains content should set the @xml:lang attribute to the language that applies to the document.

The question: How do I set the @xml:lang attribute using lxml? Everyone seems confused on the forums.

The answer: It’s a special case.


from lxml import etree

tree = etree.parse('<map><topicref href="someFile"/><topicref href="otherFile"/></map>')

root = tree.getroot()
attr = root.attrib
attr['{http://www.w3.org/XML/1998/namespace}lang'] = "en-us"

That’s it.  Once you know the trick, it’s fairly easy to script for your translation needs.

Spread the word.

Leave a Reply

Your email address will not be published. Required fields are marked *


*