user3869703
user3869703

Reputation: 3

Doing the sum of multiple value

In want to add value of price of every node in xsl . but those value can also have -9756vc(eg) type of value that need not to be included in the sum that then it give NaN as result that i dont want . I am not able to figure our how to do that . below is the sample XML :

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>-9756vc</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>

The code which I am trying .

     <?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Title</th>
        <th style="text-align:left">Artist</th>`enter code here`
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
     <xsl:variable name="color" select="format-number(price,'0.##')" /> 
 <xsl:choose>
      <xsl:when test="$color = 'NaN' ">
       <td> <xsl:value-of select="'0'"/> </td>
       </xsl:when>
      <xsl:otherwise>
         <td><xsl:value-of select="$color"/></td>
      </xsl:otherwise>
      </xsl:choose>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
<xsl:for-each select="catalog/cd">
<xsl:variable name="color" select="format-number(price,'0.##')" /> 
<xsl:choose>
      <xsl:when test="$color != 'NaN' ">
 <xsl:variable name="Sum" select ="$color + 1"/>
<xsl:value-of select="$Sum"/>
</xsl:when>
      </xsl:choose>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 358

Answers (1)

Tim C
Tim C

Reputation: 70648

It may be worth pointing out that variables are immutable in XSLT, and cannot be changed once set. This means you can't iterate over the cd elements for a xsl:for-each and keep a running total.

You can, however, use the sum function, and this can be combined with an xpath expression to select only the cd elements with a numeric price. If you head over to XPath test if node value is number you will see the way to check if a node is numeric is as follows:

 number(price) = price

Therefore, to get the sum of all valid prices, you would do this

<xsl:value-of select="sum(catalog/cd[number(price) = price]/price)" />

This line would replace the second xsl:for-each loop in your XSLT. For your XML, it would return the value 19.8

Upvotes: 1

Related Questions