Template Example |
Example 13. foreach.xml
<?xml version='1.0'?> <table> <row><entry>a1</entry><entry>a2</entry></row> <row><entry>b1</entry><entry>b2</entry></row> <row><entry>c1</entry><entry>c2</entry></row> </table>
Example 14. foreach.xsl
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="table"> <table> <xsl:for-each select="row"> <tr> <xsl:for-each select="entry"> <td><xsl:apply-templates/></td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
Example 15. foreach.html
<table> <tr> <td>a1</td><td>a2</td> </tr> <tr> <td>b1</td><td>b2</td> </tr> <tr> <td>c1</td><td>c2</td> </tr> </table>
Example 16. namedtemplate.xml
<chapter> <warning> <para>Using a damaged extension cord may cause a fire.</para> </warning> <caution> <para>Freshly brewed coffee is hot.</para> </caution> </chapter>
Example 17. namedtemplate.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template name="admonition"> <xsl:param name="type">Warning</xsl:param> <table border="1"> <tr><th><xsl:value-of select="$type"/>:</th></tr> <tr><td><xsl:apply-templates/></td></tr> </table> </xsl:template> <xsl:template match="warning"> <xsl:call-template name="admonition"/> </xsl:template> <xsl:template match="caution"> <xsl:call-template name="admonition"> <xsl:with-param name="type">Caution</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template match="para"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:template match="emphasis"> <i><xsl:apply-templates/></i> </xsl:template> </xsl:stylesheet>
Example 18. namedtemplate.html
<table border="1"> <tr> <th>Warning:</th> </tr> <tr> <td> <p>Using a damaged extension cord may cause a fire.</p> </td> </tr> </table> <table border="1"> <tr> <th>Caution:</th> </tr> <tr> <td> <p>Freshly brewed coffee is hot.</p> </td> </tr> </table>
Any element in a template rule that is not in the XSL (or other extension) namespace is copied literally to the result tree
The content of <xsl:text> elements is copied directly to the result tree; whitespace is preserved by default
<xsl:text>Literal result text</xsl:text>
Inserts the value of an expression into the result tree, converting it to a string first if necessary
<xsl:value-of select="$count + 1"/>
Copies the current node or, in the case of xsl:copy-of, the selected nodes, into the result tree without first converting them to a string
<xsl:copy-of select="title"/>
Instantiates the named element
... <xsl:param name="header">h3</xsl:param> ... <xsl:element name="{$header}"> <xsl:apply-templates/> </xsl:element>
Adds the named attribute to the nearest containing element
<table> <xsl:if test="@pgwide='1'"> <xsl:attribute name="width">100%</xsl:attribute> </xsl:if> ... </table>
Simple conditional (no "else")
<xsl:if test="$somecondition"> <xsl:text>this text only gets used if $somecondition is true()</xsl:text> </xsl:if>
Select among alternatives with <xsl:when> and <xsl:otherwise>
<xsl:choose> <xsl:when test="$count > 2"><xsl:text>, and </xsl:text></xsl:when> <xsl:when test="$count > 1"><xsl:text> and </xsl:text></xsl:when> <xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise> </xsl:choose>
To report errors, use <xsl:message>.
<xsl:message> <xsl:text>Error: no ID found for linkend: </xsl:text> <xsl:value-of select="@linkend"/> <xsl:text>.</xsl:text> </xsl:message>
<xsl:variable> allows you to associate a variable with a string, node list, or result tree fragment.
Variables are "single assignment" (no side effects)
Variables are lexically scoped
Variables assigned inside a conditional only apply inside that conditional. This is rarely correct:
<xsl:if test="$foo"> <xsl:variable name="bar">...</xsl:variable> </xsl:if>
Usually, you want to put the conditional inside the variable
<xsl:variable name="bar"> <xsl:if test="$foo">...</xsl:if> </xsl:variable>
After variables (or parameters) have been declared, they can be used in two places:
In XSL element attributes that expect an expression. These are summarized in the following table:
XSLT Element | Attribute |
---|---|
xsl:apply-templates | select |
xsl:value-of | select |
xsl:number | value |
xsl:for-each | select |
xsl:if | test |
xsl:when | test |
xsl:sort | select |
In attribute value templates; attribute value templates have the form "{$variable}". They are allowed in the following places:
Element | Attribute |
---|---|
Literal result elements | any attribute |
xsl:element | name |
namespace | |
xsl:attribute | name |
namespace | |
xsl:number | level |
count | |
from | |
format | |
lang | |
grouping-separator | |
grouping-size | |
xsl:sort | order |
lang | |
data-type | |
case-order | |
xsl:processing-instruction | name |
To insert a literal "{" character in a context where attribute value templates are expanded, use "{{".