Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a node using XPath if sibling node has a specific value?

Tags:

xml

xpath

I have the following document:

<a>   <bb>abc</bb>   <cc>ccc</cc>   <dd>ddd</dd> </a> <a>   <bb>zz</bb>   <cc>1</cc>   <dd>2</dd> </a> 

How can I get the value of <cc> using XPath if <bb> is zz?

like image 259
HOE SENGKIANG Avatar asked Jun 11 '13 09:06

HOE SENGKIANG


People also ask

How does XPath select sibling elements?

Select all A sibling elements that precede the context node. > Select all A sibling elements that follow the context node. > Select all sibling elements that precede the context node. > Select the first preceding sibling element named A in reverse document order.

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.

How do you select the second element with the same XPath?

//div[@class='content'][2] means: Select all elements called div from anywhere in the document, but only the ones that have a class attribute whose value is equal to "content". Of those selected nodes, only keep those which are the second div[@class = 'content'] element of their parent.


1 Answers

Not sure why everybody is querying for siblings, you can also check for <bb/>-elements matching the predicate from <a/>'s predicate:

//a[bb/text() = "zz"]/cc/text() 
like image 179
Jens Erat Avatar answered Sep 19 '22 01:09

Jens Erat