XPath selecting a node with some attribute value equals to some other node's attribute value

Xpath

Xpath Problem Overview


<grand id="grand">
  <parent>
    <child age="18" id="#not-grand"/>
    <child age="20" id="#grand"/> <!-- This is what I want to locate -->
  </parent>
</grand>

Can anybody tell me how to express for locating the second child?

This doesn't work...

"/grand/parent/child[@id=concat('#',/grand/@id)]/@age"

Thank you.


I'm sorry. The expression is OK. I found I got some problems in other area not the expression itself.

Xpath Solutions


Solution 1 - Xpath

This XPath is specific to the code snippet you've provided. To select <child> with id as #grand you can write //child[@id='#grand'].

To get age //child[@id='#grand']/@age

Hope this helps

Solution 2 - Xpath

I think this is what you want:

/grand/parent/child[@id="#grand"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJin KwonView Question on Stackoverflow
Solution 1 - XpathVaman KulkarniView Answer on Stackoverflow
Solution 2 - XpathMarcoSView Answer on Stackoverflow