Transformations of XML data March 17, 2013 1 The XSLT language 1.1 Context, history • 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. 1.2 The main principles • 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. 1.3 Main principles (2) • 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. 1 1.4 The main sources of information - specifications, references, tutorials, FAQ • XSLT 1.0 W3C Recommendation: http://www.w3.org/TR/xslt • What is XSLT? na XML.COM: http://www.xml.eom/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 2 Syntaxe XSLT 2.1 The 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! 2.2 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 2.3 XSLT templates • 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 xshcall-template. 2 3 Semantics of XSLT 3.1 XSLT - input 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. 3.2 XSLT - template 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. 3.3 XSLT - specification 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.). 3.4 XSLT - outputting text nodes How to produce a text node: 1. Insert the text into the template body. Note the whitespaces! (space, tab, CR/LF)! 2. Use special elt. jxshtext^text nodej/xsl:text£. Whitespaces are preserved. 3 4 Implicit/default templates 4.1 Implicit templates 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 4.2 Implicit templates overview (1) • " Default tree (do-nothing) traversal": • " Default tree (do-nothing) traversal for specified mode": 4.3 Implicit templates overview (2) • "Copy text nodes and attributes" into the result tree fragment: • " Ignore Pis and comments": 5 Generating values 5.1 Generate element with given attributes Goal: Generate given element (with a priori known name) but calculated attribute values.Solution: Use literal result element as usually - and specify att. values in so-called attribute value templates (AVT): Template: 4 . .. 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' 5.2 Element with both attributes and name generated Task: Generate element with run-time generated name, attribute names, value...How-to:Use xsl:element: ... Sablona: IDK/xsl: attribute> Creates element elt_name with attribute id="IDl" . 6 XSLT - conditional processing 6.1 Flow-control inside template - conditional parts Simple conditional output by xshif ... Template adding content if price I 2:

Drahy rohlik - cena Kc

6.2 Flow-control inside template - branching ... ... ...

Drahy 5 Podezfele levny Bezny rohlik - cena Kc

Filters out two extreme prices - for xshotherwise a normal price remains. 6.3 Flow-control inside template - loops ... ... ...

Rohlik - cena K5

For each "rohlik" generates a paragraph with info on "rohlik" and its price. Note: The xsl:for-each has a typical procedural character, it is good to use it less frequently, as it is too tightly bound to the precise input structure. 7 Advanced topics 7.1 Processing modes Modes allow to have more templates with the same match patterns for different purposes, eg.: • one for generating ToC of a document • second for generating full-text of the document One can switch on the mode when using apply-templates with attribute mode: • if mode= is present, only templates with the same mode will match • if not present, only templates without any mode attribute will match 7.2 Declaring and calling named templates Declaration - xsl: template name=" jmeno_sablony"Might contain parameters: • Calling - might specify actual parameter values: • or • hodnota parametru 6 7.3 Automated (generated) numbering /l If we add the element xsl:number into the template body, a number given by a counter will be produced.The counter can be specified: • ordinal number of the source element within its parent - also multi-level, like (sub)chapter 1.1. etc. 7.4 Automated (generated) numbering /2 If we apply the style:
7.5 Automated (generated) numbering /3 to the following source file: Bugs Bunny 03/21/1970 3K/age>
4895 Wabbit Hole Road
865-111-111K/phone>
Daisy Duck 08/09/1949 5K/age>
748 Golden Pond
865-222-2222
Minnie Mouse 04/13/1977 24
4064 Cheese Factory Blvd
865-333-3333 7
7.6 Automated (generated) numbering /4 it gains the resulting HTML page (the indentation might differ...) l. Bugs Bunny
2. Daisy Duck
3. Minnie Mouse
7.7 Automatic numbering (2)

Moje nova kniha Prvni kapitola Prvni sekce prvni kapitoly Text 8 Druhá sekce první kapitoly Text druhé sekce Druhá kapitola První sekce druhé kapitoly Text Druhá sekce druhé kapitoly Text druhé sekce