Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL sum of attribute values problem URGENT

Status
Not open for further replies.

dnml

Technical User
Mar 4, 2003
7
AU
Hello, Being a newbie to the world of xml and xsl I am having difficulty creating a stylesheet to sum attribute values from my input xml.

The input xml has multiple Field nodes that each have a name attribute that I want to match to then get the value attribute and sum with all other occurances of that name attribute match throughout the input xml.

I can get the name attribute match to work but not the sum. The output I am getting is the value of each occurance rather than the sum. How do I get it to sum correctly do the output is just a summary of values?

Here is my input XML:

<RESPONSE>
<FEATURES>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="64.1"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="43.8"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="1141"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="72.56"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="45.2"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="1641"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="47.7"/>
<FIELD name="AVERAGE_AG" value="4.3"/>
<FIELD name="AVERAGE__1" value="41.3"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="1889"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="75.96"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="42"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="3191"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="72.56"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="45.2"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="3406"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="72.56"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="45.2"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="3424"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="64.1"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="43.8"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="3564"/>
</FIELDS>
</FEATURE>
<FEATURE>
<FIELDS>
<FIELD name="WEIGHTED_L" value="72.56"/>
<FIELD name="AVERAGE_AG" value="0"/>
<FIELD name="AVERAGE__1" value="45.2"/>
<FIELD name="_INVESTME" value="0"/>
<FIELD name="_ARREARS_" value="0"/>
<FIELD name="#SHAPE#" value="[Geometry]"/>
<FIELD name="#ID#" value="3696"/>
</FIELDS>
</FEATURE>
<FEATURECOUNT count="8" hasmore="false"/>
</FEATURES>
</RESPONSE>

Here is my XSL so far:

<?xml-stylesheet type="text/xsl" href="C:\jakarta-tomcat\webapps\sdsi\external\get_features_to_html.xsl"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:eek:utput method="html"/>
<xsl:param name="index" select="1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="FIELD">
<xsl:if test="@name='WEIGHTED_L' ">Weighted LVR: <xsl:value-of select="sum(@value)"/>
<br/>
</xsl:if>
<xsl:if test="@name='AVERAGE_AG' ">Average Age: <xsl:value-of select="sum(@value)"/>
<br/>
</xsl:if>
<xsl:if test="@name='AVERAGE__1' ">Average Loan Age: <xsl:value-of select="sum(@value)"/> (years)<br/>
</xsl:if>
<xsl:if test="@name='_INVESTME' ">Investment: <xsl:value-of select="sum(@value)" />
<br/>
</xsl:if>
<xsl:if test="@name='_ARREARS_' ">Arrears: <xsl:value-of select="sum(@value)"/>
<br/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


Thanks in advance for any assistance.

Dan
 
The way you do it, your context is just one FIELD-node; there is only one attribute value in that context.
What you need to do is start at a node 'higher up' in the xml-tree, and then sum for alle value-attributes of all FIELD-nodes with the same name-attribute.
An example (a bit overdone, but just as an illustration):
Code:
  <xsl:template match="FEATURES">
   <table border="1">
     <tr>
      <td>fieldname</td>
      <td>sum</td>
      <td>count</td>
      <td>average</td>
      <td>rounded average</td>
     </tr>
     <tr>
      <td>Sum of Field 'Weighted LVR'</td>
      <td>
        <xsl:value-of select="sum(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value)" />
      </td>
      <td>
        <xsl:value-of select="count(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value)" />
      </td>
      <td>
        <xsl:value-of select="sum(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value) div count(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value)" />
      </td>
      <td>
        <xsl:value-of select="round(100 * sum(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value) div count(FEATURE/FIELDS/FIELD[@name='WEIGHTED_L']/@value)) div 100" />
      </td>
     </tr>
   </table>
  </xsl:template>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top