Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » XML » XSLT
Language: XML, XSLT
Expertise: Advanced
Jan 12, 2004

Grouping and Summing the Values in XML Using XSLT

In this column, I have grouped the XML based on the GroupID element by using the generate-id() function. This generates unique keys used for indexing the key value. The key value is GroupID.

All the PV values of each groupID is then added and the sum is formatted till six decimal values. Here's the XSLT file:


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method = "text"  omit-xml-declaration="no" standalone="no"
indent="yes"  />
	<xsl:key name = "keyGroupID"  match ="RiskRow" use = "GroupID" />
	<xsl:template match="/">
		<xsl:for-each select =
"//RiskRow[generate-id(.)=generate-id(key('keyGroupID',GroupID)[1])]">
			<xsl:variable name="lngGroupID"><xsl:value-of
select="GroupID"/></xsl:variable>
			<xsl:text>The groupID is =</xsl:text>
			<xsl:value-of select="$lngGroupID" />
			<xsl:text>|</xsl:text>
			<xsl:variable name="1stTradeID"
select="//RiskRow[GroupID=$lngGroupID]"/>
			<xsl:call-template name="ShowEmployeesInTeam">
				<xsl:with-param name="1stTradeID" select="$1stTradeID" />
			</xsl:call-template>
			<!--Put carriage return and linefeed character -->
			<xsl:text>

</xsl:text>
		</xsl:for-each>
	</xsl:template>

	<xsl:template name="ShowEmployeesInTeam">
		<xsl:param name="1stTradeID"/>
		<!--Show the details of Employee currently being processes -->
		<xsl:for-each select="$1stTradeID">
			<!--Include the columns here-->
			<xsl:value-of select="TradeID" /> <xsl:text> </xsl:text>
			<xsl:value-of select="PVinEuro1" /><xsl:text>|</xsl:text>
		</xsl:for-each>
		<xsl:text>Sum is = </xsl:text>
		<!--Summation on PVinEuro1 and format the number till 6 decimal point-->
		<xsl:value-of
select='format-number(sum($1stTradeID/PVinEuro1),"###############.######")
' />
	</xsl:template>
</xsl:stylesheet>
Now, input the XML file:

<?xml version="1.0"?>
<MarketConformity>
<RiskRow><GroupID>244133L</GroupID><TradeID>244133L_EUR_STUB</TradeID><PVi
nEuro1>-11121619.203</PVinEuro1></RiskRow>
<RiskRow><GroupID>325866L</GroupID><TradeID>325866L_EUR_STUB</TradeID><PVi
nEuro1>-12121619.2938</PVinEuro1></RiskRow>
<RiskRow><GroupID>325867L</GroupID><TradeID>325867L_EUR_STUB</TradeID><PVi
nEuro1>-14121619.293864</PVinEuro1></RiskRow>
<RiskRow><GroupID>502722L</GroupID><TradeID>502722L_EUR_STUB</TradeID><PVi
nEuro1>-13121543.293643</PVinEuro1></RiskRow>
<RiskRow><GroupID>502722L</GroupID><TradeID>502722L_PLN_STUB</TradeID><PVi
nEuro1>-11234329.698236</PVinEuro1></RiskRow>
<RiskRow><GroupID>325866L</GroupID><TradeID>506593L_EUR_STUB</TradeID><PVi
nEuro1>-11134543.12963</PVinEuro1></RiskRow>
<RiskRow><GroupID>244133L</GroupID><TradeID>542056L_EUR_STUB</TradeID><PVi
nEuro1>-12342342.769823</PVinEuro1></RiskRow>
<RiskRow><GroupID>325867L</GroupID><TradeID>553257L_PLN_STUB</TradeID><PVi
nEuro1>-98745908.128361</PVinEuro1></RiskRow>
<RiskRow><GroupID>570079L</GroupID><TradeID>570079L</TradeID><PVinEuro1>-6
4325623.693286</PVinEuro1></RiskRow>
<RiskRow><GroupID>570079L</GroupID><TradeID>570084L_EUR_STUB</TradeID><PVi
nEuro1>-67824233.986723</PVinEuro1></RiskRow>
<RiskRow><GroupID>244133L</GroupID><TradeID>570090L</TradeID><PVinEuro1>-5
6721463.972345</PVinEuro1></RiskRow>
<RiskRow><GroupID>325866L</GroupID><TradeID>570096L</TradeID><PVinEuro1>-8
7623763.23442</PVinEuro1></RiskRow>
<RiskRow><GroupID>325867L</GroupID><TradeID>570098L</TradeID><PVinEuro1>-7
9843267.4232</PVinEuro1></RiskRow>
<RiskRow><GroupID>244133L</GroupID><TradeID>570099L</TradeID><PVinEuro1>-8
7246234.23423</PVinEuro1></RiskRow>
<RiskRow><GroupID>325866L</GroupID><TradeID>570106L</TradeID><PVinEuro1>-3
2423896.4732</PVinEuro1></RiskRow>
</MarketConformity>
Siddhartha Singh
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap