Python XpathEvaluator without namespace

I need to write a dynamic function that finds elements in a subtree of an ATOM XML document.

To do this, I wrote something like this:

tree = etree.parse(xmlFileUrl) e = etree.XPathEvaluator(tree, namespaces={'def':'http://www.w3.org/2005/Atom'}) entries = e('//def:entry') for entry in entries: mypath = tree.getpath(entry) + "/category" category = e(mypath) 

The code above cannot find the category.

The reason is that getpath returns XPath without namespaces, while XPathEvaluator e () requires a namespace.

Is there a way to get getpath to return namespaces in a path, or to let XPathEvaluator accept a path without specifying a namespace (or rather, specify it in some other way)?

+3
source share
1 answer

Using

 *[local-name() = 'category'] 

Or, if you want to be more precise :

 *[local-name() = 'category' and namespace-uri() = 'http://www.w3.org/2005/Atom'] 

Or simply :

 def:category 
+2
source

Source: https://habr.com/ru/post/1442946/


All Articles