Comparison operator : Comparison operator « XSLT stylesheet « XML






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()"