Skip to content

Instantly share code, notes, and snippets.

@datalove
Last active August 26, 2015 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalove/b24dd49342e506964e99 to your computer and use it in GitHub Desktop.
Save datalove/b24dd49342e506964e99 to your computer and use it in GitHub Desktop.
XPath to select parent where children meet certain criteria

For the code below, if we want to select records have a field with colour = Red and length > 30...

<root>
<record id="1">
  <field id="colour" value="Red" />
  <field id="size"   value="Small" />
  <field id="length" value=25 />
</record>
<record id="2">
  <field id="colour" value="Red" />
  <field id="size"   value="Large" />
  <field id="length" value=50 />
</record>
</root>

Then we case use the following XPath1.0:

record[field[@id='colour' and @value='Red'] and field[@id='length' and @value > 30]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment