dampee
dampee

Reputation: 3447

XPath: select parent node when child propertyvalue starts with value

I need to select all parent nodes of type FOO where BAR nodes are childs and the BAR node has a attribute A that starts with X.

e.g. In the example below, i would like to select FOO (id = A) because i have a BAR with attribute A starting with an X.

<rumba>
  <latin>
    <FOO id="A">
      <BAR id="1" A="XYZ" />
      <BAR id="2" A="ABC" />
    </FOO>
  </latin>
  <salsa>
    <FOO id="B">
      <BAR id="3" A="UVW" />
    </FOO>
  </salsa>
</rumba>

I am pretty sure this can be done in XPATH, but I can't get my head wrapped around this one.

Upvotes: 0

Views: 915

Answers (2)

dariom
dariom

Reputation: 4588

I haven't test these but I think they should work.

If your context node is a BAR you can use this to find a parent with characteristics you described:

../FOO[BAR[starts-with(@A, "X")]]

Or if you want to search the entire document for FOO nodes that match your criteria you can use:

//FOO[BAR[starts-with(@A, "X")]]

Upvotes: 1

user3942918
user3942918

Reputation: 26413

//FOO[BAR[starts-with(@A, 'X')]]

Upvotes: 4

Related Questions