advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 5/5 | Rate this item | 1 user has rated this item.
Expertise: Advanced
Language: XML, XSLT
January 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
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs