On 10/03/2026 22:37, Jens Tröger via lxml - The Python XML Toolkit wrote:
     >>> xml.xpath("//*[name() = 'w’]")  # Any element node whose tag is ‘w’, 
ignoring the ns

The comment is not correct and the advice is dodgy: `name()` is the qualified name, so it's only going to "ignore the ns" for elements which are specifically not using a namespace prefix:

>>> etree.fromstring('<w xmlns="http://www.tei-c.org/ns/1.0"/>').xpath('name(/*)')
'w'
>>> etree.fromstring('<t:w xmlns:t="http://www.tei-c.org/ns/1.0"/>').xpath('name(/*)')
't:w'

If you actually want to ignore the ns, you should use `local-name()`, that behaves consistently:

>>> etree.fromstring('<t:w xmlns:t="http://www.tei-c.org/ns/1.0"/>').xpath('local-name(/*)')
'w'

You can use namespace-uri() alongside it:

>>> etree.fromstring('<w xmlns="http://www.tei-c.org/ns/1.0"/>').xpath('namespace-uri(/*)')
'http://www.tei-c.org/ns/1.0'
>>> etree.fromstring('<t:w xmlns:t="http://www.tei-c.org/ns/1.0"/>').xpath('namespace-uri(/*)')
'http://www.tei-c.org/ns/1.0'
_______________________________________________
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