Call defined template : call template « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="US-ASCII"?>

<state name="BC">
 <county>Bristol<population>1</population></county>
 
 <county>Kent</county>
 <county>Newport</county>
 <county>Providence</county>
 <county>Washington</county>
</state>



File: Transform.xslt

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="state">
    <xsl:call-template name="nl" />
    <xsl:text>Counties of </xsl:text>
    <xsl:value-of select="@name" />
    <xsl:call-template name="nl" />
    <xsl:text>Description: </xsl:text>
    <xsl:value-of select="description" />
    <xsl:call-template name="nl" />
    <xsl:text>Source: </xsl:text>
    <xsl:value-of select="from" />
    <xsl:call-template name="nl" />
    <xsl:text>URL: </xsl:text>
    <xsl:value-of select="url" />
    <xsl:call-template name="nl" />
    <xsl:apply-templates select="county" />
    <xsl:text>Estimated state population: </xsl:text>
    <xsl:value-of select="sum(county/population)" />
    <xsl:call-template name="nl" />
  </xsl:template>

  <xsl:template match="county">
    <xsl:text> - </xsl:text>
    <xsl:value-of select="@name" />
    <xsl:text>: </xsl:text>
    <xsl:value-of select="population" />
    <xsl:apply-templates select="population" />
  </xsl:template>

  <xsl:template match="population" name="nl">
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>
Output:



Counties of BC
Description: 
Source: 
URL: 
 - : 1
 - :  - :  - :  - : Estimated state population: 1








5.36.call template
5.36.1.Call defined template
5.36.2.use parameter with template
5.36.3.Return value from template
5.36.4.While XSLT does not define while and for loops, their behavior can be simulated.