alisikone
alisikone

Reputation: 1

XPath - find parent from child value

Need the xpath query to find element all element c's that have element t value of "string goes here"

<f>
  <c>john</c>
  <cr>100</cr>
  <lb>
    <l num="1">
      <d>12-FEB-2015</d>
      <ts>
        <t>string goes here</t>

Have tried several variations of

- "./c[./t=""string goes here""]"

Upvotes: 0

Views: 491

Answers (1)

Rubens Farias
Rubens Farias

Reputation: 57996

You should go with

"//c[.//t[.='string goes here']]"

Explanation:

  • //c a c element, doesn't matters where in the tree
  • .//t with a child t element, does't matters how deep it is

The important concept here it's the . axis, representing the current context; in the .//t it keeps track of parent element context, looking for t elements inside it.

If you t element location is immutable, you can also do:

"//c[lb/l/tr/t[.='string goes here']]"

Upvotes: 1

Related Questions