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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math Averages with XSL

Status
Not open for further replies.

crazysexycrazy

Technical User
May 21, 2007
5
AU
Hi, i want to get a average Score from the three different User Groups, how can i do this? I know how to add and subtract but find it difficult with the id attributes... Would it be something like this?
<xsl:for-each select="//User[@id='001']">

<xsl:value-of select="UserGroups/UserGroup@code="A001" + UserGroups/UserGroup@code="A002""/> ...etc? or what? i'm kind of confused

Also i want to add All the usergroups worth elements up? how do i do that? It just trying to add with the id attribute that is confusing me

<Users>
<User id="001">
<User_Details>
<User_Name>John</User_Name>
<User_Number1>360</User_Number1>
<User_Number2>300</User_Number2>
<Difference>60</Difference>
<TotalWorth>160</TotalWorth>
</User_Details>
<UserGroups>
<UserGroup code="A001">
<Name>A1</Name>
<Score>50</Score>
<Worth>15</Worth>
</UserGroup>
<UserGroup code="A002">
<Name>A1</Name>
<Score>50</Score>
<Worth>15</Worth>
</UserGroup>
<UserGroup code="A003">
<Name>A1</Name>
<Score>50</Score>
<Worth>15</Worth>
</UserGroup>
</UserGroups>
</User>
</Users>

 
Finding the average involves adding the possibly unknown number of <Score> element values. It is the latter that is difficult, as you have discovered.

You can find two different ways of adding multiple element values in this thread: thread426-1363579. In particular, the post dated [tt]7 May 07 18:46[/tt] starts the part of the discussion dealing with using the sum() function, which is intolerant of nonnumerics in your data, versus using recursion to add the numbers that do exist while ignoring the others.

Study the math part of the example in that thread (and ignore the grouping, unless that pertains to your solution as well) and apply it to your problem. If you have difficulty, please post your XSLT so we might better be able to determine what might be wrong.

Tom Morrison
 
OK, but the real trouble i'm having is adding up all the values of worth, i dont know how to do it with the id attributes? Any ideas?

Would it be?

<xsl:for-each select="//User[@id='001']">

<xsl:value-of select="UserGroups/UserGroup@code="A001/Worth" + UserGroups/UserGroup@code="A002/Worth""/> ...etc?
 
Did you read the previously referenced thread? It deals directly with this (now restated) issue.

The sum() function has as its argument a nodeset, so you need to think in terms of, "How do I specify an XPath expression that will select all the nodes the values of which I wish to sum?" You are thinking in a 'procedural' manner when you consider xsl:for-each. xsl:value-of is the right element, but you must get the XPath expression in the select attribute correct. What does the following XPath expression select?
Code:
/Users/UserGroups/UserGroup/Worth

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top