XSL Free Tutorial

Web based School

Template Example


Next Next


For-each 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>

Named Template Example

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>

Creating the Result Tree

Literal Result Elements

Any element in a template rule that is not in the XSL (or other extension) namespace is copied literally to the result tree

<xsl:text>

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>
<xsl:value-of>

Inserts the value of an expression into the result tree, converting it to a string first if necessary

<xsl:value-of select="$count + 1"/>
<xsl:copy> and <xsl:copy-of>

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"/>
<xsl:element>

Instantiates the named element

...
<xsl:param name="header">h3</xsl:param>
...
<xsl:element name="{$header}">
  <xsl:apply-templates/>
</xsl:element>
<xsl:attribute>

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>

Conditional Processing

<xsl:if>

Simple conditional (no "else")

<xsl:if test="$somecondition">
  <xsl:text>this text only gets used if $somecondition 
  is true()</xsl:text>
</xsl:if>
<xsl:choose>

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>

Declaring Variables

  • <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>

Using Variables

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 ElementAttribute
    xsl:apply-templatesselect
    xsl:value-ofselect
    xsl:numbervalue
    xsl:for-eachselect
    xsl:iftest
    xsl:whentest
    xsl:sortselect

  • In attribute value templates; attribute value templates have the form "{$variable}". They are allowed in the following places:

    ElementAttribute
    Literal result elementsany attribute
    xsl:elementname
    namespace
    xsl:attributename
    namespace
    xsl:numberlevel
    count
    from
    format
    lang
    grouping-separator
    grouping-size
    xsl:sortorder
    lang
    data-type
    case-order
    xsl:processing-instructionname

  • To insert a literal "{" character in a context where attribute value templates are expanded, use "{{".





Next Next