template with parameters : template match « XSLT stylesheet « XML






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