tags:

views:

107

answers:

2

how to find greatest 2 numbers out of 3 numbers and perform some arithmetic operations. like in following example.

Example

<root>
    <num>10</num>
    <num>12</num>
    <num>8</num>
</root>

for the above code xslt should display "10 + 12 = 22" and "average = 11"

help me to do it.

Thanks in advance!!!

+4  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>
    <xsl:template match="/root">
        <xsl:variable name="max1" select="num[not(../num > .)]"/>
        <xsl:variable name="max2" select="num[not(../num[count(.|$max1)!=1] > .)]"/>
        <xsl:value-of select="concat($max1,' + ',
                                     $max2,' = ',
                                     $max1 + $max2,'&#xA;',
                                     'average = ',
                                     ($max1 + $max2) div 2,'&#xA;')"/>
    </xsl:template>
</xsl:stylesheet>

Output:

12 + 10 = 22
average = 11
Alejandro
@Alejandro: Thanks :-)
brainless
@brainless: You're wellcome.
Alejandro
+3  A: 

I. XSLT 1.0 solution

This transformation finds the sum and average of all numbers except those with the minimal value -- works for a nodeset of numbers of any length:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:variable name="vMin">
  <xsl:for-each select="/*/num">
   <xsl:sort data-type="number"/>
   <xsl:if test="position()=1">
    <xsl:value-of select="."/>
   </xsl:if>
  </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vNumsWithoutMin" select="/*/num[not(.=$vMin)]"/>

 <xsl:variable name="vSumWithoutMin" select="sum($vNumsWithoutMin)"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="$vNumsWithoutMin"/>
  <xsl:value-of select="concat(' = ', $vSumWithoutMin)"/>
  average = <xsl:value-of select=
  "$vSumWithoutMin div count($vNumsWithoutMin)"/>
 </xsl:template>

 <xsl:template match="num">
   <xsl:value-of select="."/>
   <xsl:if test="not(position()=last())">
     <xsl:text> + </xsl:text>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

When this is applied on the provided XML document:

<root>
    <num>10</num>
    <num>12</num>
    <num>8</num>
</root>

the wanted, correct result is produced:

10 + 12 = 22
average = 11

II. XSLT 2.0 solution

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
 <xsl:output method="text"/>

 <xsl:variable name="vMin" select="min(/*/num/number(.))"/>

 <xsl:variable name="vNumsSansMin"
  select="/*/num[not(number() eq $vMin)]/number(.)"/>
 <xsl:variable name="vAvgSansMin"
      select="avg($vNumsSansMin)"/>

 <xsl:template match="/*">
    <xsl:sequence select=
     "(for $i in 1 to count($vNumsSansMin)
        return
         ($vNumsSansMin[$i],
          if(not($i eq count($vNumsSansMin)))
            then ' + '
            else ()
          )
      ),
      ' = ', sum($vNumsSansMin)
     "/>

     average = <xsl:sequence select="$vAvgSansMin"/>
 </xsl:template>
</xsl:stylesheet>
Dimitre Novatchev
@Dimitre Novatchev: Thanks a lot :-)
brainless