template with parameters : template match « 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 » template match 
template with parameters


File: Data.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<booklist>
   <book>
      <title>title 1</title>
      <author>author 1</author>
      <publisher>publisher 1</publisher>
      <isbn>1-11-11111-1</isbn>
      <price>6.99</price>
      <sales>235</sales>
   </book>
   <book>
      <title>title 2</title>
      <author>author 2</author>
      <publisher>publisher 2</publisher>
      <isbn>0 14 018967 X</isbn>
      <price>12.99</price>
      <sales>12</sales>
   </book>
</booklist>


File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet 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="get-min-and-max">
    <xsl:param name="nodes" as="node()*" />
    <xsl:param name="min-so-far" select="number('INF')"
      as="xs:double" />
    <xsl:param name="max-so-far" select="number('-INF')"
      as="xs:double" />
    <xsl:choose>
      <xsl:when test="$nodes">
        <xsl:call-template name="get-min-and-max">
          <xsl:with-param name="nodes"
            select="remove($nodes, 1)" />
          <xsl:with-param name="min-so-far"
            select="if (number($nodes[1]) lt $min-so-far) 
                                  then $nodes[1]
                                  else $min-so-far" />
          <xsl:with-param name="max-so-far"
            select="if (number($nodes[1]) gt $max-so-far) 
                                  then $nodes[1]
                                  else $max-so-far" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:document>
          <min>
            <xsl:value-of select="$min-so-far" />
          </min>
          <max>
            <xsl:value-of select="$max-so-far" />
          </max>
        </xsl:document>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
    <result>
      <xsl:call-template name="get-min-and-max">
        <xsl:with-param name="nodes"
          select="/booklist/book/price" />
      </xsl:call-template>
    </result>
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><result><min>6.99</min><max>12.99</max></result>

 
Related examples in the same category
1. Define and use template
2. Locate parent tags and get value from children tags
3. Select value from an element with value-of
4. Get two values in one template
5. match an element
6. output in template
7. set match mode to fulltext
8. match and get value operations with namespace
9. Call a template with parameter
10. template mode="index"
11. output a table without loop
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.