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

XSL transformation question

Status
Not open for further replies.

psynergy

Programmer
Joined
Jul 24, 2002
Messages
1
Location
US
Hello!

I have the following document structure

<Items>
<Item>
<Type>foo</Type>
<Value>1.00</Value>
</Item>
<Item>
<Type>bar</Type>
<Value>-2.00</Value>
</Item>
<Item>
<Type>foo</Type>
<Value>1.00</Value>
</Item>
<!-- more <Item>s here -->
</Items>

To summarize, <Items> can contain any number of <Item>
tags. Each <Item> tag contains <Type> and <Value> tags.

What I need to do is the following. Given value of &quot;foo&quot;,
I need to find all <Item> tags with <Type> set to &quot;foo&quot;
and print out the sum of all <Value>s.

Is this something that can be done? I can't figure this
out for the life of me... heh

Thanks!
 
The simplest approach will be to use the style sheet below:
_________________________________________________________

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:output method=&quot;html&quot; version=&quot;4.0&quot; />
<xsl:template match=&quot;/&quot;>
<xsl:apply-templates />
</xsl:template>
<xsl:template match=&quot;Items&quot;>
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match=&quot;Item&quot;>
<xsl:variable name=&quot;type&quot; select=&quot;Type&quot; />
<xsl:number format=&quot;1. &quot; />
<xsl:value-of select=&quot;$type&quot; />
<xsl:value-of select=&quot;' sum = '&quot; />
<xsl:value-of select=&quot;sum(/Items/Item/Value[../Type=$type])&quot; />
<br />
</xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top