xpathxml-parsingxpath-2.0

Xpath inside xpath


I am trying to create an xpath for the below scenario, could you please help ?

How can i find out the value of C whose @parent is /A/B/@id, that is PQR, C may not be the immediate next node . I want to evaluate result=/A/B/@id and then give the result as input to /A/C[@parent="result"]..is it possible?

<A>
 <B id="1">W</B>
 <C parent="1">PQR</C>
 <C parent="2">ABC</C>
</A>

Solution

  • Yes, it's possible.

    With a predicate filter expression, you can test whether the @parent value of /A/C is equal to the @id value of the /A/B element and it will return the C element that satisfies that criteria.

    /A/C[@parent=/A/B/@id]