Getting element's name in XPATH
Asked Answered
G

2

40

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

Giess answered 2/11, 2011 at 17:14 Comment(0)
V
74

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'])

*If using 2.0+, the expression //element/*[@id='elid'] must only return one element. Otherwise you'll get an error like A sequence of more than one item is not allowed as the first argument of fn:name()

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

Vrablik answered 2/11, 2011 at 17:16 Comment(0)
P
2

The tag names tree can also be obtained with

echo "du //Element/*" | xmllint --shell response-02.xml
Ele1
    id
    name
    Nested1
        id
        name
Ele2
Photooffset answered 26/6, 2015 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.