The root element xsl: transform or xsl: stylesheet encloses the whole XSLT
style and NS specifies the prefix for the XSLT elements.
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:
In the root element:
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:
How to produce a text node:
Implicit templates are defined by the specification and are implemented by
any conformant XSLT processor in order to:
<xsl:template match="*|/">
<xsl:apply-templates/>
<xsl:template>
<xsl:template match="*|/" mode="...">
<xsl:apply-templates mode="..."/>
<xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
<xsl:template>
<xsl:template match="processing-instruction()|comment()" />
<link ref="odkaz_dovnitr_dok">
...
</link>
<xsl:template match="link">
<a href="#{@ref}"> ... </a>
</xsl:template>
<generate element="elt_name"> ... </generate>
<xsl:template match="generate">
<xsl:element name="{@element}">
<xsl:attribute name="id">ID1</xsl:attribute>
</xsl:element>
</xsl:template>
<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>
<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.
<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>
<xsl:param name="jmenoParametru"/>
Calling - <xsl:call-template name="jmenoSablony">
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>
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>
Example 3. Automated numbering according to element position
<html>
<body>1. Bugs Bunny<br>
2. Daisy Duck<br>
3. Minnie Mouse<br>
</body>
</html>
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
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
Example 6. Resulting HTML page
image:images/book-html.png