XML technologies / XSLT / XSLT 3.0
(The following XSLT 3.0 material was kindly provided by students of Hochschule der Medien Stuttgart for the “Markup Languages und Anwendungen” seminar on January 16, 2016. We thank Philipp Burger, Jonas Häfele, Lena Krächan and Arno Pitters for their successful presentation on XSLT streaming and the illustrative exercises.)
XSLT 3.0 was developed together with XPath 3.0. Both use the new XDM 3.0 data model (XQuery and XPath Data Model 3.0). This is a joint standards update. Assigning individual features to one language is difficult: they span languages, usually interact and build on each other.
Some functions formerly defined in XSLT are now defined in the library XPath and XQuery Functions and Operators 3.0.
The main new XSLT 3.0 features are:
Higher-order functions
XPath 3.0 permits anonymous functions, and XDM 3.0 gained an element function. This permits lambda functions such as (x, y) # x*x + y*y. See the example below.
Code example: Variable f1 is assigned an anonymous function taking an integer and returning a function, which itself takes and returns an integer.
<xsl:template match="/">
<xsl:variable name="f1" select="
function($x as xs:integer) as (function(xs:integer) as xs:integer){
function ($y as xs:integer) as xs:integer{
$x*$x + $y * $y
}
}
"/>
<xsl:value-of select="$f1(10)(3)"/>
</xsl:template>Line 2: Define variable f1 and assign an anonymous function.
Line 3: Function declaration. Two anonymous functions are defined, one returned by the other.
Line 4: Declare the second function, taking y as an integer parameter and returning an integer.
Line 5: The function body computes and sums powers of the two parameters.
Line 9: Call the function with values "10" and "3".
Some standard lambda functions, such as map, filter, fold-left, fold-right or mappairs, are already in the function library.
Text manipulation
Text functions for formatting numbers, dates and times existed in XSLT 1.0, then moved to XPath for use in XQuery too. XPath 2.0 added numerous functions: tokenize, matches, replace, string-join, upper-case and lower-case. In XPath and XQuery Functions and Operators 3.0, further functions help parse CSV files (unparsed-text-lines, unparsed-text-available). See the example below.
Code example: A CSV header is processed using function unparsed-text-lines($csv).
<xsl:param name="csv" select="'test.csv'"/>
<xsl:param name="seperator" select="','"/>
<xsl:variable name="head" select="tokenize(unparsed-text-lines($csv)[1], seperator)"/>Maps
Maps are a new data type extending XPath 3.0’s type system. New XPath functions and XSLT constructs support them.
A map always consists of a “Key” and a “Value”. Duplicate keys are forbidden. A map behaves similarly to a HashMap in Java.
A map function returning the value for a $key:
map:get($map, $key)
Further map functions:
map:merge($maps as map(*)*) as map(*)map:size($input as map(*)) as xs:integermap:contains($map as map(*),$key as xs:anyAtomicType) as xs:booleanmap:put($map as map(*),$key as xs:anyAtomicType,$value as item()*) as map(*)map:remove($map as map(*),$key as xs:anyAtomicType) as map(*)
Streaming
This material focuses on XSLT streaming, detailed on the following pages. New elements and their functions are explained through extensive examples and exercises illustrating their use. See XSLT streaming.