I could not find a way to get the tag name other than the names from the element - lxml considers the full part of the namespace of the tag name. Here are some options that may help.
You can also use the QName class to create a tag with names for comparison:
import lxml.etree from lxml.etree import QName tree = lxml.etree.fromstring('<root xmlns:f="foo"><f:test/></root>') qn = QName(tree.nsmap['f'], 'test') assert tree[0].tag == qn
If you need a bare tag name, you need to write a utility function to extract it:
def get_bare_tag(elem): return elem.tag.rsplit('}', 1)[-1] assert get_bare_tag(tree[0]) == 'test'
Unfortunately, as far as I know, you cannot search for tags with "any namespace" (for example, {*}test ) using the lxml xpath / find methods.
Updated . Please note: lxml will not create a tag containing only { or } - it will lead to the creation of a ValueError: invalid tag name, so we can safely assume that the element with the tag name starts with { balanced.
lxml.etree.Element('{foo') ValueError: Invalid tag name
samplebias
source share