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!

SUM /group by question 1

Status
Not open for further replies.

taupirho

Programmer
Jun 25, 2002
680
0
0
GB
Hi, complete newbie from XSL perspective and would appreciate any help on the following:

Given the XML below what XSL would I need to do an equivalent of the SQL

SUM(quantity) group by id

e.g

<parts>
<part><id>123<\id><quantity>200</quantity></part>
<part><id>123<\id><quantity>100</quantity></part>
<part><id>456<\id><quantity>900</quantity></part>
<part><id>678<\id><quantity>600</quantity></part>
<part><id>123<\id><quantity> 50</quantity></part>
</parts}

to give output something like

ID=123 quantity=350
ID=456 quantity=900
ID=678 quantity=600




In order to understand recursion, you must first understand recursion.
 
These are the two ingredients: the key and the template for text output.
[tt]
<xsl:key name="idxid" match="parts/part/id" use="." />

<xsl:template match="parts">
<xsl:for-each select="part/id[count(.|key('idxid',.)[1]) = 1]">
<xsl:text>ID=</xsl:text><xsl:value-of select="." />
<xsl:text>&#x09;</xsl:text>
<xsl:text>quantity=</xsl:text><xsl:value-of select="sum(key('idxid',.)/parent::*/quantity)" />
<xsl:text>&#x0d;&#x0a;</xsl:text>
</xsl:for-each>
</xsl:template>
[/tt]
 
Have a well deserved star tsuji - works like a charm


In order to understand recursion, you must first understand recursion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top