Skip to content Skip to sidebar Skip to footer

Is It A Xpath (lxml) Bug?

I have my xpath: //*[namespace-uri() = 'http://foundation.org/UA/2011/03/NodeSet.xsd'][local-name() = 'Reference'][@ReferenceType = 'HasNotifier']/../../Description[@Locale='en']

Solution 1:

It is not an lxml bug. Here is an XPath expression that works:

//*[local-name() = 'Reference'][@ReferenceType = 'HasNotifier']/../../*[local-name() = 'Description'][@Locale='en']

You don't need namespace-uri() = ... but you have to use *[local-name() = 'Description'] instead of just Description.

Another way to do it is this. Declare a prefix for the namespace URI and use it in the expression:

from lxml import etree

NS = {'ns': 'http://foundation.org/UA/2011/03/NodeSet.xsd'}      
doc = etree.parse("nodeset.xml")
expr = "//ns:Reference[@ReferenceType = 'HasNotifier']/../../ns:Description[@Locale = 'en']"print(doc.xpath(expr, namespaces=NS)[0].text)

Output:

002CC-ESSO01.(WAAA05.01?1)

Post a Comment for "Is It A Xpath (lxml) Bug?"