Osagie Odigie
Osagie Odigie

Reputation: 291

getting parent attribute from a known child's value

Trying to select an element's attriibute based on the value of one or more of it's children, no matter how deep.

Thinking of the following but not working, appreciate any help, thanks !

"//*[node()[contains(text(), 'FirstInfo')] and node()[contains(text(), 'SecondInfo')]/::parent]"

this is the XML:

<root>
<a ID="GetThisText">
  <b>
   <c>FirstInfo</c>

   <d>SecondInfo</d>
</b>
</a>
</root>

Upvotes: 1

Views: 102

Answers (2)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 184965

Try this :

'//*[contains(., "FirstInfo")][contains(., "SecondInfo")]/ancestor::*/@ID'

or better :

'//*[c="FirstInfo" and d="SecondInfo"]/ancestor::a/@ID'

Upvotes: 0

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

Use the following path expression

//a[descendant::c = 'FirstInfo' and descendant::d = 'SecondInfo']/@ID

which translates to

//a                                  Find all elements `a` no matter where in the XML
                                     tree
[descendant::c = 'FirstInfo'         But only return them if they have a descendant
                                     element 'c' with the value "FirstInfo"
and descendant::d = 'SecondInfo']    and a descendant element "d" with the value
                                     "SecondInfo"
/@ID                                 of those elements `a` return the element `ID`.

and results in

ID="GetThisText"

Upvotes: 1

Related Questions