The XSLT language - Context, history

The main principles

Main principles (2)

The main sources of information - specifications, references, tutorials, FAQ

Syntaxe XSLT - 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:

Overall structure of an XSLT stylesheet

In the root element:

XSLT templates

Semantics of XSLT - XSLT - input document processing

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:

XSLT - specification of template output

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. <xsl:text>text node</xsl:text>. Whitespaces are preserved.

Implicit templates

Implicit templates are defined by the specification and are implemented by
any conformant XSLT processor in order to:

Implicit templates overview (1)

 <xsl:template match="*|/">
     <xsl:apply-templates/>
 <xsl:template>
 <xsl:template match="*|/" mode="...">
     <xsl:apply-templates mode="..."/>
 <xsl:template>

Implicit templates overview (2)

 <xsl:template match="text()|@*">
     <xsl:value-of select="."/>
 <xsl:template>
 <xsl:template match="processing-instruction()|comment()" />

Generate element with given attributes

 <link ref="odkaz_dovnitr_dok">
    ...
 </link>
 <xsl:template match="link">
    <a href="#{@ref}"> ... </a>
 </xsl:template>

Element with both attributes and name generated

 <generate element="elt_name"> ... </generate>
 <xsl:template match="generate">
    <xsl:element name="{@element}">
       <xsl:attribute name="id">ID1</xsl:attribute>
    </xsl:element>
 </xsl:template>

Flow-control inside template - conditional parts

 <rohlik cena="5"> ... </rohlik>
 <xsl:template match="rohlik">
    <p>
       <xsl:if test="cena>2">
          <span class="expensive">Drahy</span>
       </xsl:if> rohlik - cena <xsl:value-of select="@cena"/> Kc </p>
 </xsl:template>

Flow-control inside template - branching

 <rohlik cena="5"> ... </rohlik>
 <rohlik cena="2"> ... </rohlik>
 <rohlik cena="0.9"> ... </rohlik>

 <xsl:template match="rohlik">
    <p>
       <xsl:when test="cena>2">
          <span class="expensive">Drahy</span>
       </xsl:when>
       <xsl:when test="cena<1">
          <span class="strangely-cheap">Podezrele levny</span>
       </xsl:when>
       <xsl:otherwise>
          <span class="normal-price">Bezny</span>
       </xsl:otherwise> rohlik - cena <xsl:value-of select="@cena"/> Kc </p>
 </xsl:template>
Filters out two extreme prices - for xsl:otherwise "a normal" price
remains.

Flow-control inside template - loops

 <pecivo>
    <rohlik cena="5"> ... </rohlik>
    <rohlik cena="2"> ... </rohlik>
    <rohlik cena="0.9"> ... </rohlik>
 </pecivo>

 <xsl:template match="pecivo">
    <xsl:for-each select="rohlik">
       <p>Rohlik - cena <xsl:value-of select="@cena"/> Kc</p>
    </xsl:for-each>
 </xsl:template>

Advanced topics - Processing modes

Declaring and calling named templates

    <xsl:param name="jmenoParametru"/>
Calling - <xsl:call-template name="jmenoSablony">

Automated (generated) numbering (1)

Automated (generated) numbering (2)

Example 1. Automated numbering according to element position
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">
    <xsl:template match="/">
       <html>
          <body>
             <xsl:for-each select="devguru_staff/programmer">
                <xsl:number value="position()" format="1. " />
                <xsl:value-of select="name" />
                <br/>
             </xsl:for-each>
          </body>
       </html>
    </xsl:template>
 </xsl:stylesheet>

Automated (generated) numbering (3)

Example 2. Automated numbering according to element position
 <devguru_staff>
     <programmer>
         <name>Bugs Bunny</name>
         <dob>03/21/1970</dob>
         <age>31</age>
         <address>4895 Wabbit Hole Road</address>
         <phone>865-111-1111</phone>
     </programmer>
     <programmer>
         <name>Daisy Duck</name>
         <dob>08/09/1949</dob>
         <age>51</age>
         <address>748 Golden Pond</address>
         <phone>865-222-2222</phone>
     </programmer>
     <programmer>
         <name>Minnie Mouse</name>
         <dob>04/13/1977</dob>
         <age>24</age>
         <address>4064 Cheese Factory Blvd</address>
         <phone>865-333-3333</phone>
     </programmer>
 </devguru_staff>

Automated (generated) numbering (4)

Example 3. Automated numbering according to element position
 <html>
     <body>1. Bugs Bunny<br>
           2. Daisy Duck<br>
           3. Minnie Mouse<br>
     </body>
 </html>

Automatic numbering (2)

Example 4. Automatic multi-level numbering style
 <xsl:stylesheet
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         version="1.0">
     <xsl:template match="/book">
         <html>
             <body>
                 <xsl:for-each select="chapter">
                     <h2>
                         <xsl:number count="chapter" format="1. "/>
                         <xsl:value-of select="title" />
                     </h2>
                     <xsl:for-each select="sect1">
                         <h3>
                             <xsl:number count="chapter" format="1. "/>
                             <xsl:number count="sect1" format="a. "/>
                             <xsl:value-of select="title" />
                         </h3>
                         <xsl:apply-templates select="para"/>
                     </xsl:for-each>
                 </xsl:for-each>
             </body>
         </html>
     </xsl:template>
 </xsl:styleshee

Automatic numbering (3)

Example 5. Source file
 <book>
     <title>Moje nova kniha</title>
     <chapter>
         <title>Prvni kapitola</title>
         <sect1>
             <title>Prvni sekce prvni kapitoly</title>
             <para>Text</para>
         </sect1>
         <sect1>
             <title>Druha sekce prvni kapitoly</title>
             <para>Text druhe sekce</para>
         </sect1>
     </chapter>
     <chapter>
         <title>Druha kapitola</title>
         <sect1>
             <title>Prvni sekce druhe kapitoly</title>
             <para>Text</para>
         </sect1>
         <sect1>
             <title>Druha sekce druhe kapitoly</title>
             <para>Text druhe sekce</para>
         </sect1>
     </chapter>
 </book

Automatic numbering (4)

Example 6. Resulting HTML page

image:images/book-html.png

What is recommended?

Reusability of styles

Design patterns

XSLT Processors

Advanced topics