Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting element's name in XPATH

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

like image 943
Ariyan Avatar asked Nov 02 '11 17:11

Ariyan


People also ask

How do I find the XPath of a tag name?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

What is local name () in XPath?

The local-name function returns a string representing the local name of the first node in a given node-set.


2 Answers

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).

like image 160
Daniel Haley Avatar answered Nov 08 '22 12:11

Daniel Haley


The tag names tree can also be obtained with

echo "du //Element/*" | xmllint --shell response-02.xml Ele1     id     name     Nested1         id         name Ele2 
like image 39
LMC Avatar answered Nov 08 '22 11:11

LMC