call-template with parameter : call template « XSLT stylesheet « XML






call-template with parameter



File: Data.xml


<?xml version="1.0"?>
<SCENE>
  <TITLE>title</TITLE>
  <STAGEDIR>A</STAGEDIR>
  <SPEECH>
    <SPEAKER>C</SPEAKER>
    <LINE>B</LINE>
    <LINE>C</LINE>
    <LINE>D</LINE>
  </SPEECH>
</SCENE>
File: Transform.xslt

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs" version="2.0">
  <xsl:template name="longest-speech" as="element(SPEECH)?">
    <xsl:param name="list" as="element(SPEECH)*" />
    <xsl:choose>
      <xsl:when test="$list">
        <xsl:variable name="first" select="count($list[1]/LINE)"
          as="xs:integer" />
        <xsl:variable name="longest-of-rest"
          as="element(SPEECH)?">
          <xsl:call-template name="longest-speech">
            <xsl:with-param name="list"
              select="$list[position()!=1]" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
          <xsl:when
            test="$first gt count($longest-of-rest/LINE)">
            <xsl:sequence select="$list[1]" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:sequence select="$longest-of-rest" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
    <longest-speech>
      <xsl:call-template name="longest-speech">
        <xsl:with-param name="list" select="//SPEECH" />
      </xsl:call-template>
    </longest-speech>
  </xsl:template>
</xsl:transform>


Output:

<?xml version="1.0" encoding="UTF-8"?><longest-speech><SPEECH>
    <SPEAKER>C</SPEAKER>
    <LINE>B</LINE>
    <LINE>C</LINE>
    <LINE>D</LINE>
  </SPEECH></longest-speech>

 








Related examples in the same category

1.Call template with parameters
2.template that does the replacement
3.One template calls another template
4.Call template twice