Skip to content Skip to sidebar Skip to footer

The Limit Of Element Tree On Xpath

I've used Element Tree for a while and i love it because of its simplicity But I'm doubting of its implementation of x path This is the XML file <

Solution 1:

ElementTree provides limited support for XPath expressions. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the core library.

(F. Lundh, XPath Support in ElementTree.)

For an ElementTree implementation that supports XPath (1.0), check out LXML:

>>> s = """<a>
  <b name="b1"></b>
  <b name="b2"><c /></b>
  <b name="b2"></b>
  <b name="b3"></b>
</a>""">>> from lxml import etree
>>> t = etree.fromstring(s)
>>> t.xpath("b[@name='b2' and c]")
[<Element b at 1340788>]

Solution 2:

From the ElementTree documentation on XPath support.:

ElementTree provides limited support for XPath expressions. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the core library.

You've just discovered a limitation in the implementation. You could use lxml instead; it provides a ElementTree-compatible interface with complete XPath 1.0 support.

Post a Comment for "The Limit Of Element Tree On Xpath"