XSLT Workshop: Basic XPath Functions Crib Sheet

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/.

String functions (operations on text)

normalize-space( string )
Trims leading and trailing spaces, and collapses all internal sequences of spaces to a single space.
 normalize-space('  this   has lots    of spaces    ') 
   returns:
   "this has lots of spaces"
contains( haystack, needle )
Returns true or false, depending on whether the first string contains the second string or not.
 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( string, pattern, replacement )
A regular-expression replace function. Returns a copy of string in which all instances of the pattern have been replaced by the replacement.
 replace('XPath is cool.', 'cool', 'very cool') 
   returns:
   "XPath is very cool."
starts-with( string1, string2 )
Returns true if string1 starts with string2, and false if not.
 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( string1, string2 )
Returns true if string1 ends with string2, and false if not.
 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.

Functions for handling sequences

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( sequence… )
This returns a sequence of only the distinct values in a sequence. In other words, it removes duplicates.
 distinct-values(//div/@type)
   returns:
   a list of the unique values of the @type attribute on <div> elements.
empty( sequence… )
Returns true or false depending on whether the sequence is empty.
 empty(//div[parent::back])
   returns:
   true() if there are any <div> elements in the <back>,
   false() if not.

Mathematical functions

count( sequence… )
Counts the items fed to it, and returns the total.
 count(//div) 
avg( sequence… )
Calculates the average from a sequence of numbers.
 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( sequence… )
Calculates the sum of a list of numbers.
 sum((count(//person), count(//org))) 
Note the proliferating parentheses...
min( sequence… )
Returns the smallest from a list of numbers.
 min(//person/age/@value) 
max( sequence… )
Returns the largest a list of numbers.
 max(//person/age/@value) 
round( number )
Rounds a number to the nearest integer. .5 is rounded up.
 round(1.6)
   returns:
   2

Boolean functions (dealing with true or false values)

not( expression )
Returns true if the expression is false, and false if it is true.
 not(count(//p) gt 5)
   returns:
   false() if there are 6 or more <div> in the document,
   true() if there are 5 or fewer.
true(), false()
true() and false() are functions in XPath. if you're checking the result of a boolean operation, don’t forget the parentheses!
 <xsl:if test="(count(//div) lt 5) = false()"> 

Context functions

position( node )
Returns the position of the node in its sequence.
 //div[position() = 1]
   returns:
   all <div>. elements which are the first <div> in their parent element.
last()
returns the number of the last item in the sequence.
 //div[position() = last()]
   returns:
   all <div>. elements which are the last <div> in their parent element.