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

Adding xml element values

Status
Not open for further replies.

acsooley

Programmer
Nov 13, 2002
32
0
0
CA
Have a xml doc like:

<home>
<tag>4</tag>
<tag>6</tag>
<tag>5</tag>
<tag>5</tag>
<tag>10</tag>
</home>

Is there a way to add the values in xsl to give me the total of tag? So it would output 30?

Adam
 
[tt]<xsl:template match="home">
<xsl:value-of select="sum(//tag)" />
</xsl:template>[/tt]
 
Sorry I should have done this differently:

<home>
<ppl>
<var>a</var>
<tag>4</tag>
</ppl>
<ppl>
<var>c</var>
<tag>6</tag>
</ppl>
<ppl>
<var>a</var>
<tag>5</tag>
</ppl>
<ppl>
<var>d</var>
<tag>5</tag>
</ppl>
<ppl>
<var>c</var>
<tag>10</tag>
</ppl>
</home>

So if I am doing a search for the c var I want the total of the tag tags? Which would be 16. I would also like to divide it if it is possible. So get the total of 16 and divide by 2 to get 8.

Any help?

Thanks
Adam
 
[tt]<xsl:template match="home">
<xsl:value-of select="sum(//tag[preceding-sibling::var='c'])" />
</xsl:template>[/tt]
The rest, go figure out yourself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top