Next / Previous / Contents / TCC Help System / NM Tech homepage

7.10. The XMLID() function: Convert text to XML with a dictionary of id values

To convert XML in the form of a string into an Element structure, use Section 7.4, “The fromstring() function: Create an element from a string”. However, there is a similar function named etree.XMLID() that does this and also provides a dictionary that allows you to find elements in a tree by their unique id attribute values.

The XML standard stipulates that any element in any document can have an id attribute, but each value of this attribute must be unique within the document. The intent of this feature is that applications can refer to any element using its id value.

Here is the general form for this function:


The return value is a tuple (E, D), where:

Here's a small example script:

#!/usr/bin/env python
from lxml import etree

SOURCE = '''<dog id="Fido">
<cat id="Fluff">Mao?</cat>
<rhino id="ZR"/>
tree, idMap = etree.XMLID(SOURCE)

for id in sorted(idMap.keys()):
    elt = idMap[id].text or "(none)"
    print "Tag {0}, text is '{1}'".format(id, elt.strip())

And its output:

Tag Fido, text is 'Woof!'
Tag Fluff, text is 'Mao?'
Tag ZR, text is '(none)'