XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / XPath – Filtering sequences / XPath functions in predicates
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)
XPath expressions forming predicates can use functions to state conditions. Especially useful are functions reporting on the current context, called context functions here. All other XPath and XSLT functions are allowed too, particularly some string functions and sequence functions.
XPath functions – a brief look at syntax
First, a brief overview of function-call syntax. What does a function look like?
XPath functions generally use this syntax inside expressions:
fn:funktionsname()
As in most languages, parentheses follow the name and can receive arguments to process:
fn:funktionsname(argument1, argument2)
Every function has a return value. After evaluation, the processor replaces the call with its result. The call therefore acts as a placeholder for that value.
<xsl:value-of select="fn:funktionsname()"/>
therefore corresponds to
<xsl:value-of select="rückgabewert"/>
where the result may be an atomic value, node or sequence. Sometimes it is an empty sequence or empty string, both neutral and harmless in the relevant context.
The function name may be qualified, using a namespace prefix. XPath functions use the prefix fn with this namespace declaration:
xmlns:fn="http://www.w3.org/2005/xpath-function"
Processors usually recognize XPath functions by unqualified names too. To use a prefix, however, declare its namespace in the stylesheet.
Also note, though not yet important here, the typing of function arguments: XPath 2.0 functions require specific input types and report type errors otherwise. XPath 1.0’s automatic conversions behind the scenes no longer apply. In return, the result reliably has a specific type too.
Context functions: fn:position() and fn:last()
Processing often requires knowing a sequence’s item count and the current item’s position: its context. For a sequence of <buch> elements, it may matter how many books to output and whether the current one is last.
XPath provides two functions for this: fn:position() and fn:last().
fn:position()
returns an integer giving the current item’s position in the current sequence.
fn:last()
returns an integer giving the last item’s position, equivalent to the item count.
Both are frequently needed in predicates and elsewhere, such as numbering, as shown below.
To filter out all but the first five books, use this predicate:
<xsl:apply-templates select="buch[fn:position() < 6]"/>
Here the sequence to which fn:position() applies is first built by the location step selecting all books. The predicate then retains the first five <buch> elements.
To keep only the last five books, write:
<xsl:apply-templates select="buch[fn:position() > last()-6]"/>
Subtraction has higher precedence than comparison, so parentheses are unnecessary.
Returning to the issue of multiple indices, fn:position() solves it easily. For example,
buch[fn:position()=1 or fn:position()=3]
selects the first and third books, and
buch[fn:position() >1 and fn:position() < 6]
selects books two through five.
Boolean functions: fn:not()
The function fn:not() negates its argument and can formulate predicates. If its argument is true, it returns false, and conversely:
buch[fn:not(buchautor='Panter, Peter' or @kat='reisen')]
This path selects books whose author is not Peter Panter and whose category is not 'reisen'.
The function is rarely essential; often invert its argument instead. Here that is:
buch[buchautor!='Panter, Peter' and @kat!='reisen']
String functions: fn:contains(), fn:starts-with(), fn:ends-with()
XPath string functions can test node string values. Especially useful in predicates are fn:contains() and fn:starts-with(), and occasionally fn:ends-with(). All accept two strings and return a Boolean. For example,
buch[fn:contains(buchautor[1], 'Hans')]
can select books with a <buchautor> child whose string value contains 'Hans'. This matches 'Hass, Hans' or 'Hansen, Carl'.
For filtering by initial letters, fn:starts-with() may be more useful.
buch[fn:starts-with(buchautor[1], 'A')]
returns books whose author’s surname begins with A, given this author-element structure.
Note:
The additional inner predicate buchautor[1] is needed because fn:contains() and fn:starts-with() expect exactly one string as their first argument. Filter out any second or third author element to ensure processing succeeds.
Regular-expression functions: fn:matches()
More complex conditions require regular expressions, supported by several XPath 2.0 functions. For predicates, use fn:matches(), returning true when the pattern matches. Thus
buch[fn:matches(buchautor[1], '^[A-H].*ss[a-z]+')]
selects books whose first author has a surname, at the start of its string value, beginning with A through H and containing a double S followed by at least one letter. This matches 'Assmann, Peter', 'Esser, Klaus', 'Hesse, Hermann', but not 'Hass, Hans', where the second S is followed by a comma, not a letter.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn