Comparison operator : Comparison operator « 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 » Comparison operator 
Comparison operator



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="minMax">
    <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="count($nodes) gt 1">
        <xsl:variable name="first">
          <xsl:call-template name="minMax">
            <xsl:with-param name="nodes"
              select="subsequence($nodes, 1, count($nodes) idiv 2)" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="second">
          <xsl:call-template name="minMax">
            <xsl:with-param name="nodes"
              select="subsequence($nodes, (count($nodes) idiv 2) + 1)" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:document>
          <min>
            <xsl:value-of
              select="if(number($first/min) lt number($second/min))
                                    then $first/min
                                    else $second/min" />
          </min>
          <max>
            <xsl:value-of
              select="if(number($first/max) gt number($second/max))
                                    then $first/max
                                    else $second/max" />
          </max>
        </xsl:document>
      </xsl:when>
      <xsl:when test="count($nodes) eq 1">
        <xsl:document>
          <min>
            <xsl:value-of select="$nodes[1]" />
          </min>
          <max>
            <xsl:value-of select="$nodes[1]" />
          </max>
        </xsl:document>
      </xsl:when>
      <xsl:otherwise>
        <xsl:document>
          <min>INF</min>
          <max>-INF</max>
        </xsl:document>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
    <result>
      <xsl:call-template name="minMax">
        <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. Compare number value
2. if test="position()!=last()"
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.