Named Template Example

Named Template Example

namedtemplate.xml

<?xml version="1.0" encoding="iso-8859-1"?>

<?xml-stylesheet type="text/xsl" href="named_temp_example.xsl"?>

<chapter>



<warning>

<para>Using a damaged extension cord may cause a fire.</para>

</warning>



<caution>

<para>Freshly brewed coffee is hot.</para>

</caution>

</chapter>

namedtemplate.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" encoding="iso-8859-1" />



<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"><br />

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