xslt and xqueryfgeorges.org/papers/fgeorges-xmlss-xslt-trends-2014.pdfthe new version of xpath, xslt...
TRANSCRIPT
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Trends in XSLT and XQuery
Florent Georges
MarkLogic
XSLT and XQuery19 September 2014
summer school
www.xmlsummerschool.com Slide 2Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Learning Objectives
1. Understand the versions of the standards
2. Learn new features3. Practice them
Get an overview of new features and possibilities of the new version of XPath, XSLT and XQueryBoth by learning theory and by trying them out directly in exercises
summer school
www.xmlsummerschool.com Slide 3Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Contents
Lecture1. Introduction2. XPath 3.03. XQuery 3.04. XSLT 3.0
Tutorial Session
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Introduction
Fashion overview
summer school
www.xmlsummerschool.com Slide 5Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Introduction
• XPath is a language on its own• With a standard library [F&O]• XQuery is a super set of XPath• XSLT uses XPath as its expression language• They are all functional languages (some prefer
to say functional-oriented)• They all use the same data model: XDM• They all use XML Schema as their data type
definition language
summer school
www.xmlsummerschool.com Slide 6Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Introduction
• XPath 2.0, XSLT 2.0 and XQuery 1.0 have been released in 2007
• XPath and XSLT 2.0 were a big improvement over the version 1.0
• XQuery was a “version-1.0 language”, but with all the past experience of XPath and XSLT
• Since then, we gathered more experience in some fields, like XML databases, and in applications written entirely in those languages
• Time for new version with major improvements
summer school
www.xmlsummerschool.com Slide 7Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Introduction
All languages will now be aligned to the same version number:
3.0
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath 3.0
The basics and the commons
summer school
www.xmlsummerschool.com Slide 9Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath 3.0
1. New convenience operators2. New functions3. EQNames4. Variable declaration5. Function items6. Inline functions7. Higher-order functions8. Partial application
summer school
www.xmlsummerschool.com Slide 10Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Convenience operators
• Concat operator: '…' || '…'
'Hello, ' || $you || '!'
==> 'Hello, world!'
concat('Hello, ', $you, '!')
==> 'Hello, world!'
summer school
www.xmlsummerschool.com Slide 11Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Convenience operators
• Mapping operator: lhs ! rhs
(: path operator is not applicable to atomic values :) (1, 2) / string(.) (: path operator is not (always) equivalent to a for loop :)
(: what does the following do? :) for $n in $nodes return string($n)
(: what does the following do? :) $nodes / string(.)
summer school
www.xmlsummerschool.com Slide 12Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Convenience operators
• The mapping operator “lhs ! rhs” is equivalent to “for $i in lhs return rhs”, with “$i” set as the context item of “rhs”
• Need to review XDM, anyone?
(: what does the following do? :) for $i in $items return string($i)
(: what does the following do? :) $items ! string(.)
summer school
www.xmlsummerschool.com Slide 13Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – New functions
• round()• format-date(), format-time()• format-number()• generate-id()• parse-xml(), serialize()• analyse-string()• Math: exp(), pi(), sqrt(), cos(), log10(), etc.• New utility higher-order functions, usual
functional utilities:– fold-left(), fold-right(), filter(), etc.
summer school
www.xmlsummerschool.com Slide 14Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – EQNames
• There is no way to bind namespace URIs to prefixes in XPath itself
• EQNames provide the namespace URI directly in the expression
• Expressions are self-contained
(: what does the following mean? :) /db:article/db:info/db:title
(: what does the following mean? :) /Q{http://docbook.org/ns/docbook}article /Q{http://docbook.org/ns/docbook}info /Q{http://docbook.org/ns/docbook}title
summer school
www.xmlsummerschool.com Slide 15Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Variable declaration
• XPath expressions are sometimes convoluted, if not impossible at all, because it is not possible to define variables
• XPath 3.0 includes let expression from XQuery
(: use variables, for readability :) let $amount := 1000 let $rate := 0.21 let $vat := $amount * $rate let $included := $amount + $vat return 'Amount is ' || $amount || ', with VAT: ' || $included
summer school
www.xmlsummerschool.com Slide 16Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Function items
• Functions are first-class citizens in XPath!• We can obtain an item which represents a
function, pass it around, call it dynamically• my:function#2 returns a function items for the
function my:function() with arity 2 (that is, with 2 parameters)
• $fun('world') calls the function referenced by the function item in $fun (must be arity 1)
• New ItemType for function items: function(xs:string?, element()+)
summer school
www.xmlsummerschool.com Slide 17Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Function items
(: function item for my:less-than($lhs, $rhs) :) (: function items do not have a string value :) my:less-than#2
(: ensure the type of $f :) $f as function(xs:integer, xs:integer) as xs:boolean
(: call dynamically $f :) $f(1, 2)
(: all together, this return true for usual my:less-than :) let $f as function(xs:integer, xs:integer) as xs:boolean := my:less-than#2 return $f(1, 2)
summer school
www.xmlsummerschool.com Slide 18Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Inline functions
• function($who) { 'Hello, ' || $who || '!' } returns a function item to an anonymous function (AKA an inline function)
• Inline functions allow actually defining functions directly in XPath
• Inline functions introduce closures in XPath• Inline functions, jointly with let, can be used to
create functions in an XPath expression
summer school
www.xmlsummerschool.com Slide 19Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Inline functions
(: looks like an "XPath module" :) let $first := function($arg as xs:string) as xs:string { ... } let $second := function($arg as xs:string) as xs:string { ... $first ... } let $third := function($fun as function(xs:string) as xs:string) as xs:string { ... $fun ... } return ( $third($first), $third($second) )
summer school
www.xmlsummerschool.com Slide 20Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Higher-order functions
• A function item can be passed as parameter to another function
• A function can return a function item as return value
• A function taking a function item as parameter, or returning a function item as return value, is a higher-order function
• A piece of the algorithm itself of a higher-order function can then be injected by the caller
summer school
www.xmlsummerschool.com Slide 21Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Higher-order functions
(: say there is a function with the following signature in :) (: the static context (in pseudo-XQuery notation here): :) declare function lib:sort( $seq as item()+, $cmp as function(item(), item()) as xs:integer ) as item()+;
(: the usual integer sort can be done with: :) lib:sort( (2, 6, 3, 7, 2, 5, 2, 7, 3, 56, 42), function($lhs as xs:integer, $rhs as xs:integer) as xs:integer { if ( $lhs eq $rhs ) then 0 else if ( $lhs lt $rhs ) then -1 else 1 })
summer school
www.xmlsummerschool.com Slide 22Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Higher-order functions
(: sorting structured nodes could be done with: :) lib:sort( doc('my-employees.xml')/*/employee, function($lhs as element(), $rhs as element()) as xs:integer { if ( $lhs/last-name lt $rhs/last-name ) then -1 else if ( $lhs/last-name gt $rhs/last-name ) then 1 else if ( $lhs/first-name eq $rhs/first-name ) then 0 else if ( $lhs/first-name lt $rhs/first-name ) then -1 else 1 })
summer school
www.xmlsummerschool.com Slide 23Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Higher-order functions
(: and with a multi-comparator function for composite keys, :) (: that returns a function item composing its parameters: :) lib:sort( doc('my-employees.xml')/*/employee, lib:multi-comparator(( function($p) { $p/last-name }, function($p) { $p/first-name })))
summer school
www.xmlsummerschool.com Slide 24Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Partial application
• Given a function of arity 2, we can create a function item of arity 1 by binding one of the parameters to a fixed value
• Call the function (or function item) by replacing the parameters you want to leave blank by “?”
• For instance: compare(?, ?, 'urn:my-collation')• This creates a new function, of arity 2, based
on the standard compare(), but with the collation param bound to 'urn:my-collation'
summer school
www.xmlsummerschool.com Slide 25Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XPath – Partial application
(: say there is a function with the following signature, :) (: to compare 2 items after a custom treatment :) declare function lib:usual-compare( $lhs as item(), $rhs as item(), $access as function(item()) as item() ) as xs:integer;
(: sorting structured nodes could be done with: :) lib:sort( doc('my-employees.xml')/*/employee, lib:usual-compare(?, ?, function($arg as element()) as xs:string { $arg/@id }))
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery 3.0
Even further
summer school
www.xmlsummerschool.com Slide 27Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery 3.0
1. Output declaration2. Switch3. Private functions4. Try/catch5. Grouping6. Windowing
summer school
www.xmlsummerschool.com Slide 28Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Output declaration
• Control the serialization options from within your queries
• Like xsl:output• Defined using the options mechanism
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "xml"; declare option output:encoding "iso-8859-1"; declare option output:indent "yes";
summer school
www.xmlsummerschool.com Slide 29Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Switch
• New switch/case instruction• Different expressions depending on a value
switch ( $unit ) case 'm' return $meters case 'yd' return $meters * 1.0936 case 'mi' return $meters * 0.00062137119 default return error(xs:QName('app:ERR001'), 'Unknown unit')
summer school
www.xmlsummerschool.com Slide 30Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Private functions
• Restrict visibility of a function to its module• Defined using the annotations mechanism
(: even if in a library module, it will not be visible : from importing modules :) declare %fn:private function my:convert-to-internal( $param as xs:string ) as xs:string { ... };
summer school
www.xmlsummerschool.com Slide 31Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Try/catch
declare namespace err = "http://www.w3.org/2005/xqt-errors";
try { 1 div 0 } catch * { $err:code || ': ' || $err:description }
• Catch any error thrown in the “try” clause• Provide information through variables in the
“catch” clause• Can catch specific errors only
declare namespace err = "http://www.w3.org/2005/xqt-errors";
try { 1 div 0 } catch * { $err:code || ': ' || $err:description }
summer school
www.xmlsummerschool.com Slide 32Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Try/catch
declare namespace err = "http://www.w3.org/2005/xqt-errors";
try { 1 div 0 } catch * { $err:code || ': ' || $err:description }
• Can have different selective “catch” clauses
declare namespace err = "http://www.w3.org/2005/xqt-errors";
try { ... } catch app:ERR001 { 'alternative result' } catch app:ERR002 | app:ERR003 { 42 } catch * { 'what did you do?!?' }
summer school
www.xmlsummerschool.com Slide 33Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Grouping
• “group by” defines a grouping variable in a FLOWR expression
• The return expression is evaluated once per group
• The grouping variable gives the value of the group key
• Range variables are bound now to sequences, corresponding to all values of that variable in that group
summer school
www.xmlsummerschool.com Slide 34Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Grouping
for $elems in <doc> <elem g="1">A</elem> <elem g="1">B</elem> <elem g="3">C</elem> <elem g="3">D</elem> <elem g="2">E</elem> <elem g="1">F</elem> <elem g="2">G</elem> </doc>/* group by $key := $elems/@g order by $key return <group num="{ $key }">{ $elems/string(.) }</group>
==> <group num="1">A B F</group> <group num="2">E G</group> <group num="3">C D</group>
summer school
www.xmlsummerschool.com Slide 35Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Windowing
• [Definition: A window is a sequence of consecutive items drawn from the binding sequence.]
• Tumbling windowing “consumes” all adjacent items of a window at once, then continues with the next window
• Sliding windowing “consumes” all adjacent items of a window, then continue with the next item (possibly already in the last window)
• Defined by declaring variables and conditions
summer school
www.xmlsummerschool.com Slide 36Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Windowing
for tumbling window $w in <doc> <elem g="1">A</elem> <elem g="1">B</elem> <elem g="3">C</elem> <elem g="3">D</elem> <elem g="2">E</elem> <elem g="1">F</elem> </doc>/* start when true() end $end next $next when $end/@g ne $next/@g return <window group="{ $end/@g }">{ $w/string(.) }</window>
==> <window group="1">A B</window> <window group="3">C D</window> <window group="2">E</window> <window group="1">F</window>
summer school
www.xmlsummerschool.com Slide 37Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XQuery – Windowing
for sliding window $w in <doc> <elem g="1">A</elem> <elem g="1">B</elem> <elem g="3">C</elem> <elem g="3">D</elem> <elem g="2">E</elem> <elem g="1">F</elem> </doc>/* start when true() end $end next $next when $end/@g ne $next/@g return <window group="{ $end/@g }">{ $w/string(.) }</window>
==> <window group="1">A B</window> <window group="1">B</window> <window group="3">C D</window> <window group="3">D</window> <window group="2">E</window> <window group="1">F</window>
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT 3.0
Package it! Stream it! Recover...
summer school
www.xmlsummerschool.com Slide 39Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT 3.0
1. Text value templates2. Generalized patterns3. Mode declaration4. Context item constraint5. Dynamic evaluation of XPath6. Try/catch7. Assertions8. Packages9. Streaming10. Accumulators11. Maps
summer school
www.xmlsummerschool.com Slide 40Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Text value templates
• Easy text node creation like in XQuery• Use { } to create text nodes in a sequence ctor• Can be enabled/disabled for compatibility• Unlike XQuery, always constructs text nodes
<xsl:template ... expand-text="yes"> <new-school>{ $who }</new-school> <old-school> <xsl:value-of select="$who"/> </old-school> </xsl:template>
summer school
www.xmlsummerschool.com Slide 41Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Text value templates
<xsl:template name="balance" expand-text="yes"> <xsl:param name="name" as="xs:string"/> <xsl:param name="id" as="xs:string"/> <xsl:param name="amount" as="xs:string"/>
<p> <xsl:text>Name=</xsl:text> <xsl:value-of select="$name"/> <xsl:text>, id=</xsl:text> <xsl:value-of select="$id"/> <xsl:text>, balance=</xsl:text> <xsl:value-of select="$amount"/> <xsl:text>.</xsl:text> </p>
<p>Name={ $name }, id={ $id }, balance={ $amount }.</p>
</xsl:template
summer school
www.xmlsummerschool.com Slide 42Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Generalized patterns
• Template rules can now match any items• They used to be restricted to nodes
<xsl:template ...> <xsl:apply-templates select="'one', 'two', 'three'"/> </xsl:template>
<xsl:template match="~xs:string"> <number>General case: { . }</number> </xsl:template>
<xsl:template match="~xs:string[. eq 'forty two']"> <forty-two>Special treatment.</forty-two> </xsl:template>
summer school
www.xmlsummerschool.com Slide 43Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Mode declaration
• Modes used to be loosely declared, as soon as a template rule declared to be in that mode
• A new mode declaration allows to set properties on modes:– Visibility (public/private/final, see below)– Default behaviour– Error and warning behaviour– Is it streamable?– Is it usable as an initial mode?– What is the type of the initial context item?
summer school
www.xmlsummerschool.com Slide 44Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Mode declaration
<!-- Follow the "Modified Identity Pattern". --> <xsl:mode name="my:copy" initial="yes" on-no-match="shallow-copy" visibility="public"/>
<xsl:template match="to-replace" mode="my:copy"> <new-stuf> <xsl:value-of select="..."/> </new-stuf> </xsl:template>
summer school
www.xmlsummerschool.com Slide 45Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – xsl:context-item
• Constraint the type of the context item in a named template
• Constraint the type of the initial context item– For a specific mode, or for the initial template
<xsl:mode name="db:format"> <xsl:context-item as="document-node(element(db:article))"/> </xsl:mode>
<xsl:template name="something"> <xsl:context-item as="element(stuff)"/> ... </xsl:template>
<xsl:template name="other-thing"> <xsl:context-item use="prohibited"/> ... cannot access the context item ... </xsl:template>
summer school
www.xmlsummerschool.com Slide 46Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – xsl:evaluate
• Allow to dynamically an XPath expression provided as a string
• To use with caution, as usual with dynamic evaluation
<xsl:template match="address-book"> <xsl:param name="sort-expr" select=" 'first-name' "/> <xsl:for-each select="contact"> <xsl:sort> <!-- the expression could refer to variables, --> <!-- passed using children xsl:with-param --> <xsl:evaluate xpath="$sort-expr" context-item="."/> </xsl:sort> </xsl:for-each> </xsl:template>
summer school
www.xmlsummerschool.com Slide 47Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Try/catch
• Catch any error thrown in the “try” clause• Provide information through variables in the
“catch” clause (shared with XQuery)• Can catch specific errors only
<xsl:stylesheet ... xmlns:err="http://www.w3.org/2005/xqt-errors">
<xsl:try select="1 div 0"> <xsl:catch select="$err:code || ': ' || $err:description"/> </xsl:try>
summer school
www.xmlsummerschool.com Slide 48Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Try/catch
• Can have different selective “catch” clauses
<xsl:stylesheet ... xmlns:app="http://example.org/ns/my-app" xmlns:err="http://www.w3.org/2005/xqt-errors">
<xsl:try> <sequence-ctor> ... </sequence-ctor> <xsl:catch errors="app:ERR001" select="'alternative res'"/> <xsl:catch errors="app:ERR002 app:ERR003" select="42"/> <xsl:catch errors="*"> <xsl:value-of select="'what did you do?!?'"/> </xsl:catch> </xsl:try>
summer school
www.xmlsummerschool.com Slide 49Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Assertions
• Allow writing assertions directly in XSLT• Assertion checking can be disabled• If the assertion fails, a dynamic error is thrown
<!-- guest star: static param for conditional compilation --> <xsl:param name="DEBUG" select="false()" static="yes"/>
<xsl:function name="my:convert" as="xs:integer"> <xsl:param name="numer" as="xs:integer"/> <xsl:param name="denom" as="xs:integer"/> <xsl:assert test="$denom ne 0" use-when="$DEBUG"/> <xsl:sequence select="$numer div $denom"/> </xsl:function>
summer school
www.xmlsummerschool.com Slide 50Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Packages
• Higher level of separation than xsl:stylesheet• Packages are comprised of stylesheets• Isolate better dependencies• Support processors for separate compilation• Provide developers with real encapsulation
mechanism• In my humble opinion the necessary step
towards really reusable and deliverable XSLT libraries
• Might support XQuery
summer school
www.xmlsummerschool.com Slide 51Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Packages
• Clear borders between “imports” (called “use package”)
• Opportunity to control what components can be overridden by “using packages”
• Opportunity to control what components are actually imported from a “used package”
summer school
www.xmlsummerschool.com Slide 52Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Packages
<xsl:package xmlns:lib="http://example.org/lib/package" xmlns:iface="http://example.org/lib/package/iface" xmlns:impl="http://example.org/lib/package/impl" name="http://example.org/lib/package" package-version="1.2" version="3.0"> <xsl:stylesheet version="3.0"> <xsl:function name="lib:..."> ... </xsl:function> </xsl:stylesheet> <xsl:expose component="function" names="lib:*" visibility="final"/> <xsl:expose component="function" names="iface:*" visibility="public"/> <xsl:expose component="function" names="impl:*" visibility="private"/> </xsl:package>
summer school
www.xmlsummerschool.com Slide 53Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Packages
<xsl:package xmlns:lib="http://example.org/lib/package" name="http://h2oconsulting.be/xyz-project/my-package" package-version="0.1" version="3.0"> <xsl:use-package name="http://example.org/lib/package" package-version="1.2"> <xsl:accept component="function" names="lib:*" visibility="private"/> </xsl:use-package> <xsl:stylesheet version="3.0"> ... </xsl:stylesheet> </xsl:package>
summer school
www.xmlsummerschool.com Slide 54Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Streaming
• The XSLT Working Group “defines” streaming as “The term streaming refers to a manner of processing in which documents (such as source and result documents) are not represented by a complete tree of nodes occupying memory proportional to document size, but instead are processed "on the fly" as a sequence of events, similar in concept to the stream of events notified by an XML parser to represent markup in lexical XML”
• ...
summer school
www.xmlsummerschool.com Slide 55Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Streaming
• Basically, streaming does not require buffering the input in memory (or not much)
• Streaming does not require buffering the output in memory (or not much)
• Streaming in XSLT 1.0 was thought to be achievable by optimization
• Streaming support in XSLT 3.0 is done by providing the processor with more information it could not easily infer
• The developer does know better, usually
summer school
www.xmlsummerschool.com Slide 56Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Streaming
• The basic idea is you can navigate the input only on the way forward
• You can access nodes only once (even of course in predicates or any other expressions, e.g. used in a condition)
• The name of the ancestor elements, and their attributes, are also available
• At any point during the processing, your are either in streaming mode or not
• When in streaming mode, the processor enforces you are not doing anything forbidden
summer school
www.xmlsummerschool.com Slide 57Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Streaming
<xsl:stylesheet version="3.0"> <xsl:template name="main"> <xsl:stream href="book.xml"> <xsl:for-each select="book"> <xsl:for-each select="chapter"> <xsl:result-document href="chapter{position()}.xml"> <xsl:copy-of select="."/> </xsl:result-document> </xsl:for-each> </xsl:for-each> </xsl:stream> </xsl:template> </xsl:stylesheet>
summer school
www.xmlsummerschool.com Slide 58Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Accumulators
• First been design to support accumulating values even in streaming mode
• Convenient for any accumulation of values• Based on pattern matching• An accumulator has an initial value• Then the value can be changed when matching
another node• For instance, in a book, numbering figures
starting at 1 again in every chapter• Value accessible via a new function (named
after the accumulator)
summer school
www.xmlsummerschool.com Slide 59Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Accumulators
<xsl:accumulator name="f:figure-nr" as="xs:integer" initial-value="0" streamable="yes"> <xsl:accumulator-rule match="chapter" new-value="0"/> <xsl:accumulator-rule match="figure" new-value="$value + 1"/> </xsl:accumulator> <xsl:mode streamable="yes"/>
<xsl:template match="figure"> <xsl:apply-templates/> <p> <xsl:text>Figure </xsl:text> <xsl:value-of select="f:figure-nr()"/> </p> </xsl:template>
summer school
www.xmlsummerschool.com Slide 60Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Maps
• AKA hash dictionaries• Sequences are flat• Maps allow to represent “structured sequences”
without the copy semantics of nodes• As everything else in XDM, maps are non
mutable objects• A map is a set of pairs “key / value”• A key is an arbitrary atomic value (such as
xs:string, xs:integer, etc.)• A value is any arbitrary sequence• A map is a function
summer school
www.xmlsummerschool.com Slide 61Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
XSLT – Maps
<xsl:variable name="v1" as="map(xs:string, item()+)" select=" map { 'blue' := 'bleu', 'green' := ( 1, 2, 3), 'red' := *[@color eq 'red'] }"/>
<xsl:variable name="v2" as="map(xs:string, item()+)" select=" map:new(( map:entry('blue', 'bleu'), map:entry('green', ( 1, 2, 3)), map:entry('red', *[@color eq 'red']) ))"/>
<xsl:sequence select="$v1('red')"/>
<xsl:sequence select="map:keys($v1)"/>
<xsl:sequence select="map:get($v1, 'blue')"/>
<xsl:sequence select="map:remove($v1, 'green')"/>
www.xmlsummerschool.com
summer school
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Questions
And answers, hopefully
summer school
www.xmlsummerschool.com Slide 63Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Questions
• Any question?• For further reference:
– http://w3.org/TR/xpath-30/– http://w3.org/TR/xpath-functions-30/– http://w3.org/TR/xquery-30/– http://w3.org/TR/xslt-30/