XPath

  • XML Path Language
  • Used to address XML data
  • Specifies the nested path to the data
  • XPath expressions yield either a set of nodes, a string, a boolean (true/false value), or a number

Syntax

  • Same basic syntax as for Unix directories ( /  .  .. )
  • Use array notation [] (1-based) to specify a specific element (when there might be more than one)
  • The expression inside [] is called the predicate
  • Use the $ prefix to refer to a variable or parameter
  • Use the @ prefix to refer to an attribute
  • A matching attribute value can be specified
  • An attribute name can be specified inside array brackets (for an element) to find an element with a matching attribute (and attribute value)
.                  : Matches the context node
.. : Matches the parent node

* : Matches any element node
node() : Matches any kind of node
@* : Matches any attribute node
// : Anywhere in the hierarchy

or, and : Logical or, and (booleans)
=, != : Equal, not equal (booleans, strings, and numbers)
<, >, <=, >= : Less than, greater than, less than or equal to, greater than or equal to (numbers)
+, -, *, div, mod : Add, subtract, multiply, floating-point divide, and modulus (remainder)

() : Use parentheses to specify operator precedence
| : Alternative (to select using more than one expression)

last() : Returns the index of the last element
position() : Returns the index position
count(...) : Returns the count of elements

Axes

self::*                   : .
parent::* : ..

child::name : name
attribute::name : @name

ancestor::name : ancestor::*[n]
ancestor-or-self:name : ancestor::*[n + 1]

descendant::name
descendant-or-self::name

following::name
following-sibling::name

preceding::name
preceding-sibling::name

Examples

@type="unordered"            : Specifies an attribute named type whose value is "unordered"

LIST/@type : Specifies the type attribute of a LIST element
LIST[@type="unordered"] : Selects all LIST elements whose type value is "unordered"

LIST[@type="ordered"][3] : Selects all LIST elements of type "ordered", and returns the third
LIST[3][@type="ordered"] : Selects the third LIST element, but only if it is of type "ordered"

/PROJECT[.="MyProject"] : Selects a PROJECT named "MyProject"
/PROJECT[STATUS] : Selects all projects that have a STATUS child element
/PROJECT[STATUS="Critical"] : Selects all projects that have a STATUS child element
with the string-value "Critical"

/*/PERSON[.="Fred"] : Matches any PERSON sub-node named "Fred"

//PARA : Selects all paragraph elements in a document
/HEAD/LIST//PARA : Selects all paragraph elements in a subtree that begins from /HEAD/LIST
PARA|LIST : Selects all PARA and LIST elements

id(...) : Returns the node with the specified id (Elements only have an ID when
the document has a DTD, which specifies which attribute has the ID type)

/HEAD[last()] : Selects the last HEAD element
/HEAD[position() <= 5] : Selects the first five HEAD elements
/HEAD[count(HEAD)=0] : Selects all HEAD elements that have no subheads

Functions

Strings

compare
compare(string1, string2)
  • Returns -1, 0, 1
string-length
string-length([string])
  • If param is omitted, returns length of string value in the current node
substring
substring(string, start [, length])
  • start is 1-based
  • If length is omitted, the rest of the string is used
<xsl:variable name="year" select="substring($date, 7, 4)" />
contains
contains(string1, string2)
  • Returns true if string2 is found within string1 (if string1 contains string2)
starts-with
starts-with(string1, string2)
  • Returns true if string2 is found at the beginning of string1
upper-case
upper-case(string)
lower-case
lower-case(string)
matches
matches(string, pattern)
replace
replace(string, pattern, replace)