Hello Martin,

If your XML document declares the namespace

    xmlns="http://www.tei-c.org/ns/1.0”

then you can use XPath in two ways to find the `w` element nodes:

    >>> xml.xpath("//tei:w", namespaces={"tei": "http://www.tei-c.org/ns/1.0"})

or

    >>> xml.xpath("//*[name() = 'w’]")  # Any element node whose tag is ‘w’, 
ignoring the ns

Hope this helps,
Jens


> On Mar 11, 2026, at 06:41, Martin Mueller <[email protected]> 
> 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]

Reply via email to