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

XML
1. CSS Style
2. SVG
3. XML Schema
4. XQuery
5. XSLT stylesheet
Java
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML » XSLT stylesheet » call template 
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
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.