This is a small subset of useful XPath 2.0 functions. For a full reference, check out http://www.w3schools.com/xpath/xpath_functions.asp and http://www.w3.org/TR/xpath-functions/.
normalize-space(' this has lots of spaces ')
returns:
"this has lots of spaces"
contains(//TEI/text/body/div[1]/head[1], 'Chapter')
returns:
true() if the first head in the first div in the body contains "Chapter",
false() if it doesn’t.
Beware: this is case-sensitive.
replace('XPath is cool.', 'cool', 'very cool')
returns:
"XPath is very cool."
starts-with(//TEI/text/body/div[1]/head[1], 'Chapter')
returns:
true() if the first <head> in the first <div> in the <body> begins with "Chapter",
false() if it doesn’t.
Beware: this is case-sensitive.
ends-with(//TEI/text/body/div[1]/head[1], 'ide')
returns:
true() if the first <head> in the first <div> in the <body> ends with "ide",
false() if it doesn’t.
Beware: this is case-sensitive.
Sequences are rather like arrays in traditional programming. In XPath, they are ubiquitous; most simple XPath expressions will return a sequence. For instance, //div/@type will return a sequence of all the type attributes on div elements in the document.
distinct-values(//div/@type)
returns:
a list of the unique values of the @type attribute on <div> elements.
empty(//div[parent::back])
returns:
true() if there are any <div> elements in the <back>,
false() if not.
count(//div)
avg(//person/age/@value)
avg((1,2,3,4,5,6))NOTE: the second example includes two sets of parentheses. The outer set contains the argument to the avg() function, and the inner set constructs a sequence which constitutes the argument to the function. In other words, avg() takes a single argument, which is a sequence; it cannot take a series of separate arguments.
sum((count(//person), count(//org)))Note the proliferating parentheses...
min(//person/age/@value)
max(//person/age/@value)
round(1.6)
returns:
2
not(count(//p) gt 5)
returns:
false() if there are 6 or more <div> in the document,
true() if there are 5 or fewer.
<xsl:if test="(count(//div) lt 5) = false()">
//div[position() = 1]
returns:
all <div>. elements which are the first <div> in their parent element.
//div[position() = last()]
returns:
all <div>. elements which are the last <div> in their parent element.