The XSLT language Syntaxe XSLT Semantics of XSLT ImplioJ PB138 - Markup Languages Tomas Pitner March 17, 2013 To i Q The XSLT language 0 Syntaxe XSLT Q Semantics of XSLT Q Implicit/default templates Q Generating values Q XSLT - conditional processing Q Advanced topics • XSLT (extensible Stylesheet Language Transformation) (http://w3.org/style/XSL) is a language for specifying transformation of XML documents on the (usually) XML outputs, or text, HTML or other output formats. • The original application area, the transformation of XML data to XSL: FO (Formatting objects), thus rendering XML. • XSLT specification was therefore part of XSL (extensible Stylesheet Language). • Later, XSL set aside and began to be seen as a universal general description language XML—^ XML (txt, HTML) transformations. • The current version is determined by the XSLT 1.0 specification. Work on version 1.1 have been pledged in favor of the development of XSLT 2.0. The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< • XSLT is a functional language, where reduction rules have the form templates, which specify how nodes in the source document override output document. • XSLT transformation specification is contained in the stylesheet element, which is an XML document in the syntax XSLT. • XSLT stylesheet contains usually a set of templates in template elements. To i The XSLT language Syntaxe XSLT Semantics of XSLT Implic Main principles I (2) 1 • The templates have a selection part (LHS of the reduction rule) and construction part representing the RHS of the rule • Selection part: the attribute match • Construction part: the body of the template element • The own transformation then means that XSLT interpreter (XSLT processor, an XSLT engine) takes the input nodes of the document, it looks to their appropriate templates -according to the match clause and produces a result corresponding to construction content of this template. To i The XSLT language Syntaxe XSLT Semantics of XSLT Implic The main sources of information - specifications, references, tutorials, FAQ • XSLT 1.0 W3C Recommendation: http://www.w3.org/TR/xslt a What is XSLT? na XML.COM: http: //www.xml.com/pub/a/2000/08/holman/index.html • Mulberrytech.com XSLT Quick Reference (2xA4, PDF): http: //www.mulberrytech.com/quickref/XSLTquickref.pdf • Dr. Pawson XSLT FAQ: http://www.dpawson.co.uk/xsl/xslfaq.html • Zvon XSLT Tutorial: http: //zvon.org/xxl/XSLTutorial/Books/Bookl/index.html To i The XSLT language Syntaxe XSLT Semantics of XSLT Implic he structure of the XSLT style The root element xsl: transform or xsl: stylesheet encloses the whole XSLT style and NS specifies the prefix for the XSLT elements. The root element is: • Parameter declarations (and their implicit value) - elt. xsl:param. • Parameters can be set when calling XSLT processor - eg java-o net.sf.saxon.Transform outfile.xml infile.xml style.xsl-Dparam = paramvalueVariables • Variables declarations - elt. xsl:variable - de facto same as parameters but not settable from outside. • It should be noted that the XSLT (without processor-specific extension) is a pure functional language, i.e. a template application does not have side effect —> variables can be assigned once, then just read! The XSLT language Syntaxe XSLT Semantics of XSLT lmplic^| Overall structure of an XSLT stylesheet In the root element: • Declaration (format) of output - elt. xsl: output • ...apart of this, also less frequently used elements appear here - see eg. documentation for SAXONu (http://saxon.sf.net) • own templates - elt. xsl:template To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< • Template is a specification which node to rewrite (transform) and how. • Which nodes to rewrite is defined in the attribute match. • The result is given in the template body. • The template can be explicitly named, in such case it can be directly called using xskcall-template. To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< document processing • First, the processor selects the document root (not the root element) - corresponding to the XPath expression / • Then the processor finds a matching template (explicit or implicit - see eg. XSLT/XPath Quick Reference (http:// www.mulberrytech.com/quickref/XSLTquickref.pdf)), where the match attribute as an XPath predicate returns true in the context of the current node ("matches" the current node). • if there are more matching templates and they cannot be distinguished/ordered by priority - an error is indicated. • if there is just one such template, it is applied, ie. its body is translated into the result tree fragment. The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< :e activation order Can be specified: Directly/explicitly calling a named template - this is an imperative approach which should be avoided. Indirectly/implicitly by activating a template by selecting elements (or other nodes) and letting the processor to find a matching template itself - functional approach -preferable. The selection of nodes is done by: • Explicitly by "select" at "apply-templates". We can select any nodes specified by the XPath expression in "select". • Implicitly, letting the processor to select nodes (no "select" at "apply-templates"). Only child elements are selected then. To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< ication of template output • The output of a template is a result tree fragmentu. • The outputs of individual templates are placed to the result tree fragment, in the order corresponding to the application order. • The output is usually generated as a stream of events (eg. SAX2), which are subsequently converted to the resulting document (using specified encoding etc.). To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< tting text nodes How to produce a text node: O Insert the text into the template body. Note the whitespaces! (space, tab, CR/LF)! Q Use special elt. jxsl:text£text nodej/xsl:text£. Whitespaces are preserved. To i The XSLT language Syntaxe XSLT Semantics of XSLT Implii Implicit templates are defined by the specification and are implemented by any conformant XSLT processor in order to: • enable traversing the document tree even in case the traversal is not explicitly defined • to define default typical actions: like ignoring comments and Pis • can be overriden by explicit template(s) with the same match= attribute To i The XSLT language Syntaxe XSLT Semantics of XSLT Implii iew (1) • "Default tree (do-nothing) traversal": • "Default tree (do-nothing) traversal for specified mode": To i The XSLT language Syntaxe XSLT Semantics of XSLT Implii iew (2) • "Copy text nodes and attributes" into the result tree fragment: • "Ignore Pis and comments": Template: ... The link will be transformed to element "a", the attribute value "href will be calculated so that it inserts before the original value of "ref <;> i -00.0 Task: Generate element with run-time generated name, attribute names, value... How-to:XSse xsl: element: ... Šablona: IDK/xsl:attribute> Creates element elt_name with attribute id="IDl" . The XSLT language Syntaxe XSLT Semantics of XSLT Impli plate - conditional parts Simple conditional output by xskif ... Template adding content if price i 2:

Drahý rohlik - cena To i The XSLT language Syntaxe XSLT Semantics of XSLT lmplic^| Flow-control inside template - branching ... ... ...

Drahy Podezfele levny Bezny rohlik - cena
PB138 - Markup Languages numbering /3 to the following source file: Bugs Bunny 03/21/1970 3K/age>

4895 Wabbit Hole Road
865-lll-llll Daisy Duck 08/09/1949 5K/age>
748 Golden Pond
865-222-2222
< n „ The XSLT language Syntaxe XSLT Semantics of XSLT Implic Automated (generated) numbering /4 it gains the resulting HTML page (the indentation might differ...) l. Bugs Bunny
2. Daisy Duck
3. Minnie Mouse

Moje nová kniha První kapitola První sekce první kapitoly</titl <para>Text</para> </sectl> <sectl> <title>Druhá sekce první kapitoly</titl <para>Text druhé sekce</para> </sectl> </chapter> <chapter> <title>Druhá kapitola < □ ► a (5 > ■^j C:\temp\Ksl\book.html - Microsoft: In... ^Jnjxj File Edit View Favorites Tools Help Search Address C:\temp\xsl\book.htrinl T ŕ^Go Links » 1. První kapitola 1. a. První sekce první kapitoly 1 Text The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< « Prefer functional approach - eg. xsl:template match= and xsl:apply-templates select= • before procedural approach - xsl:template name= and xsl:call-template name= Use the working modes ( xshtemplate ... mode= and xshapply-templates ... mode= ) modes can be combined with functional approach: • xshapply-templates select=... mode=... • xshtemplate match=... mode=... To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< What to do to achieve reusability? • Restructure styles to more, simpler files • Include them into others using xsl: include (almost like textual inclusion) • or better by xsl: import (as import preferres the directly present templates before those imported) To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< Identical transformation 1 (no root element attributes) http://wwbota.free.fr/XSLT_models/identquery.xslt Identical transformation 2 http://wwbota.free.fr/XSLT_models/identquery2.xslt Identical transformation suppressing elements no having any text nodes in //* http://wwbota.free.fr/XSLT_models/ suppressEmptyElements.xslt Replace attributes with elements http: //wwbota.free.fr/XSLT_models/attributes2elements.xslt Dtto, but placing former attributes into elements in a specific namespace xslt/attributes2elements.xslt (http://www.fi.muni. cz/~tomp/xml03/xslt/attributes2elements.xslt) Reverse transformation xslt/elements2attributes.xslt (http://www.fi.muni.cz/~tomp/xml03/xslt/ elements2attributes.xslt) To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< Popular free XSLT processors (Transformation Engines) in Java: • SAXON (author M.H.Kay) (http://saxon.sf.net) • XALAN (author Apache Software Foundation) (http://xml.apache.org/xalan-j/index.html) • ... more free and commercial XSLT tools: XML Software (http://www.xmlsoftware.com/xslt.html) To i The XSLT language Syntaxe XSLT Semantics of XSLT lmpli< XSLT Design Patterns - selection (http://www.dpawson.co.uk/xsl/sect1/N169.html) The Functional Programming Language XSLT (http://www.topxml.com/xsl/articles/fp/1.asp) To i