use parameter with template : call template « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>

<province name="BigCity">
 <city>city 1</city>
 <city>city 2</city>
 <city>city 3</city>
 <city>city 4</city>
 <city>city 5</city>
 <city>city 6</city>
 <city>city 7</city>
 <city>city 8</city>
 <city>city 9</city>
 <city>city 10</city>
 <city>city 11</city>
</province>


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="province">
    <xsl:text>BigCity Cities</xsl:text>
    <xsl:call-template name="nl">
      <xsl:with-param name="nl" select="'&#10;&#10;'" />
    </xsl:call-template>
    <xsl:apply-templates select="city" />
  </xsl:template>

  <xsl:template match="city">
    <xsl:text> -> </xsl:text>
    <xsl:value-of select="." />
    <xsl:call-template name="nl">
      <xsl:with-param name="nl" select="'&#10;'" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="city[.='city 4']">
    <xsl:text> -> </xsl:text>
    <xsl:value-of select="." />
    <xsl:call-template name="nl">
      <xsl:with-param name="nl"
        select="' (second largest city in the Yukon)&#10;'" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="city[.='city 11']">
    <xsl:text> -> </xsl:text>
    <xsl:value-of select="." />
    <xsl:call-template name="nl">
      <xsl:with-param name="nl"
        select="' (largest city in the Yukon)&#10;'" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="nl">
    <xsl:param name="nl" />
    <xsl:value-of select="$nl" />
  </xsl:template>

</xsl:stylesheet>

Output:

BigCity Cities

 -> city 1
 -> city 2
 -> city 3
 -> city 4 (second largest city in the Yukon)
 -> city 5
 -> city 6
 -> city 7
 -> city 8
 -> city 9
 -> city 10
 -> city 11 (largest city in the Yukon)








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.