Issue 23423: XPath Support in ElementTree doc omission (original) (raw)
The list of XPath supported features in section 20.5.2.2. "Supported XPath syntax" on page https://docs.python.org/3.4/library/xml.etree.elementtree.html does not list the use of a predicate based on an element value (it only list predicates based on an attribute value. However, testing in 3.4 shows that a predicate based on an element value also works.
import xml.etree.ElementTree as etree x = 'bing' y = etree.XML(x)
Find by attribute value
z = y.find('bar/baz[@y="bang"]') print(z) <Element 'baz' at 0x0000000003300368>
Find by element value
z = y.find('bar/[baz="bing"]') print(z) <Element 'bar' at 0x000000000334AD18>
Find fails with incorrect element value
z = y.find('bar/[baz="bong"]') z print(z) None
Below the line that says:
[tag] Selects all elements that have a child named tag. Only immediate children are supported.
It should say something like:
[tag="value"] Selects all elements that have a child named tag with the given value. Only immediate children are supported.