> > I assumed that the full tag name {http://www.tei-c.org/ns/1.0} might work:

Clark's notation is valid in ETPath (find/findall/iterfind), but it's not part of XPath's syntax. XPath only understands namespace prefixes (ns:nodename) so you need to provide xpath with a `namespaces` mapping, as documented in https://lxml.de/xpathxslt.html#namespaces-and-prefixes

Alternatively you can bail on the nametest by using a wildcard (`//*`), and then use `local-name()` and `namespace-uri()` in the node test in order to select the nodes you're interested in e.g.

tree.xpath('//*[namespace-uri() = "http://www.tei-c.org/ns/1.0"; and local-name() = "w"]')

On 10/03/2026 21:41, Martin Mueller wrote:
I am probably missing something trivial, but I am completely baffled  by the 
use of namespaces when using xpath with lxml.

I have a template that creates a tree from an XML document and passes it to a 
function for processing at the w level. If I change the root element of a TEI 
document into ABC (turning it into a generic XML document),the following code 
works:

for  w in tree.xpath('//w'):
   print(etree.tostring(w))

This shouldn't work in a TEI document, because the xpath method requires a 
qualified name or namespace.

I assumed that the full tag name   {http://www.tei-c.org/ns/1.0}  might work:

for  w in tree.xpath('http://www.tei-c.org/ns/1.0}w'):
  print(etree.tostring(w))

But it doesn't work. Neither does a special xmlns:tei declaration.

So I am totally baffled.  For ordinary lxml processing the ordinary mapping of 
the URI to tei would work. But it fails for xpath. The rather terse lxml manual 
was no help.


I'll be grateful for any help.



_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]

_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]

Reply via email to