home—lects—exams—hws
D2L—breeze (snow day)
lect36-xpath-functions
XPath functions
and, some functions over children
Originally based on
XML Visual Quickstart Guide
by Kevin Howard Goldberg,
and notes therefrom by Jack Davis (jcdavis@radford.edu).
Chapter 4 - XML
Location paths are used for further processing by templates (xsl:template and
xsl:apply-templates), or in test conditions
(xsl:if, and the xsl:whens inside xsl:choose).
In both of these cases, there are often times that you will not want to use all the data
in the node set returned. With XPath functions, you can apply additional
logic to these node sets to return only the data you need.
Location paths can be used to extract the contents of a node using xsl:value-of.
Remember, xsl:value-of returns the string value of the first node in a node set.
With XPath functions, you can perform further operations on that string.
Note that xsl:value-of's attribute “select”
can be any expression, not just a single node (variable):
e.g. The monument is <xsl:value-of select="round(height div 3.28)" /> meters tall.
This is the same as Java expressions, which can of course be more than a single variable;
they include constants and function-calls as well1
(e.g. wage*Math.round(40*weeksWorked)/52);
and SQL SELECT statement which can select entire
expressions instead of merely columns
(SELECT wage*round(40*weeksWorked)/52 FROM …)
XSLT functions
-
overview
and
official reference.
- Multiplying, Dividing, Adding, Subtracting
Operators -- +, -, *, div (floating-point division (!)),
mod (remainder)
Watch out for the “gotcha” of not using “/”;
that will be interpreted as part of an xpath (perhaps the start of an absolute path).
- numeric comparisons:
=
!=
>
>=
<
<=
These aren't rendering-typos — you literally include the ampersand in the less-than operator!
(Remember, XSLT code is itself valid XML):
<xsl:apply-templates select="ancient_wonders/wonder[height >= 100]" />
|
XML (Comparisons)
XSLT
- XML (Math Operations)
XSLT
- Counting Nodes, count(nodeSet)
Return the number of items in the provided node-set.
XML (counting nodes)
XSLT
- number-to-string conversion:
format-number(n,formatStr)
The formatStr follows the same conventions as java.text.DecimalFormat:
0 - for each digit that should always appear
# - for each digit that should appear when not 0
. - to separate integer part from fractional part
, - to separate groups of digits
() - to surround negative numbers
XML (Format Numbers)
XSLT
- Rounding Numbers: ceiling(), floor(), round().
Oddly, there are no “max” or “min” functions!
The hack is to sort, and then select the first (!):
<xsl:apply-templates select="ancient_wonders/wonder[]/history">
<xsl:sort select="year_built" order="descending" data-type="number"/>
<xsl:if test="position()=1">
</xsl:if>
</xsl:apply-templates>
|
- concat(str1,str2,…)
- substring(src,startIndex1,len)
- substring-after(src,target)
—return the rest of src following the
first occurrence of target.
(There is a corresponding substring-before.)
For example, substring-after('Gonzo, the magnificent', ',')
returns ' the magnificent' (though of course,
most of the time the first argument will probably be a node-select statement
like “name[@language='English']” or
“muppet/stageName” or “.”).
- contains(src, target):
Does src contain target as a substring?
- starts-with(src, target):
Does src start with target?
- Translating (mapping) characters:
translate(src, fromLetters, toLetters)
Replace any character in fromLetters
with its corresponding character in toLetters.
Example: translate( ., 'ESZaA', '3$2@4').
- Summing numbers in an entire node-set
<xsl:value-of select="sum(/ancient_wonders/wonder/height)
div
count(/ancient_wonders/wonder)" />
|
Example:
XSLT computing sum, average
XML
-
Want to write your own node-processing function?
Of course you do!
This feature wasn't in the original XSLT 1.0,
but was added to 2.0.
Alas, no major browsers actually support XSLT 2.0 natively.
However, there is apparently a
javascript plugin, Saxonica CE,
which you can use.
-
- position() — returns the index of the current node, among the node-set which contains it.
XML (Test Position)
XSLT (Test Position)
- last() — returns the index of the last node, among the node-set which contains it.
Remember that indices in XSLT/XPATH are 1-based, so last() could also be stated
as returning the size of the current node's node-set.
These two functions are a bit odd — unlike most, they don't take any input;
they use the the current node as the input.
But the weird part is, they also include the context that selected the current node.
So even if you're in the body of a for-each loop
(and the body is operating on one particular node),
the last() function is returning information how many nodes are being iterated over.
(More precisely, as the specs put it,
position and last return
“the context position (resp., size) from the expression evaluation context”).
Note:
We said that square-brackets in an XPath filter the node-set on a boolean condition.
Note that you may find examples on the web where the “condition” is
a number [n] instead of a boolean;
this is xslt short-hand2
for [position()=n].
1
Expressions in Java don't end in semicolons.
It's statements that need to end in a semicolon;
statements are themselves built out of keywords, punctuation, and expressions
(if-else statments, while-statements, block statements, etc.).
↩
2
This explanation is a bit backwards from history (originally a number was like an array-lookup,
which they then generalized to boolean filters),
however the more useful principle to remember is filter, not array-lookup;
just be aware of what the shorthand means.
↩
home—lects—exams—hws
D2L—breeze (snow day)