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

HOE SENGKIANG picture HOE SENGKIANG · Jun 11, 2013 · Viewed 92.4k times · Source

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?

Answer

Jens Erat picture Jens Erat · Jun 11, 2013

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