Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get attribute value from node using xpath?

Tags:

Totally new to xpath.

I have xml:

<Values>
   <Value Value="a" CustomAtr="1" />
   <Value Value="b" CustomAtr="2" />
   <Value Value="c" CustomAtr="3" />
</Values>

To get value by index, i can use: string(/*/Value[1]/@CustomAtr)

Is it possible to get value by key? Hot to get CustomAtr value where Value="b" ?

Thank you

like image 874
Sergejs Avatar asked Nov 02 '12 11:11

Sergejs


People also ask

How does XPath find attribute value?

We can use XPath to generate attribute expressions to locate nodes in an XML document. When there are certain uniquely recognized attribute values accessible in the container tag, we employ the XPath functions (contains or starts-with) with the attribute. The “@” symbol is used to access attributes.

How use attribute for contain in XPath?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value. Here, tag: tag is the name of the tag that contains the specific word. word: In this case, the word refers to the text that must be discovered in a specific string.

What is attribute and value in XPath?

XPath Tutorial from basic to advance level. This attribute can be easily retrieved and checked by using the @attribute-name of the element. @name − get the value of attribute "name". <td><xsl:value-of select = "@rollno"/></td> Attribute can be used to compared using operators.


1 Answers

this should do the trick:

string(//Value[@Value='b']/@CustomAttr)
like image 55
Christiaan Nieuwlaat Avatar answered Oct 11 '22 01:10

Christiaan Nieuwlaat