Getting element's name in XPATH

Ariyan picture Ariyan · Nov 2, 2011 · Viewed 94.9k times · Source

If I selected an element using XPATH how can I get its name?
I mean something like text() function in //element/[@id=elid]/text().

Answer

Daniel Haley picture Daniel Haley · Nov 2, 2011

Use name(). (Find docs for newer versions of the XPath language here.)

Here are modified versions of your example:

Works in XPath 2.0 only:

//element/*[@id='elid']/name()

Works in XPath 1.0 and 2.0:

name(//element/*[@id='elid'])

You could also use local-name() which returns the local part of the expanded name (without any namespace prefix).