XPath Expressions

The XPath language is capable of building sophisticated expressions that aren't always completely obvious. This section provides some examples of the more esoteric XPath expressions.

/guitars/guitar[model]

Identifies all guitar element nodes (under the root guitars element node) that have a child model element node.

//guitar[@type and @topic]

Identifies all guitar element nodes anywhere in the document that have both a type and topic attribute node.

guitar[(sum(*/rating) div count(*/rating)) > 4.0]

This expression identifies all guitar element nodes that are children of the context node and that have a rating average (average value of rating grandchildren element nodes) greater then 4.0.

 //guitar[/featured-model = model[1]] ...

Get Essential XML: Beyond Markup now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.