[xsl] Document summary using xsl:key

Subject: [xsl] Document summary using xsl:key
From: "Rob Anderson" <roba@xxxxxxxxxx>
Date: Thu, 24 Jul 2003 15:33:40 +0100
Help!

I am really struggling with this...

I have the following XML

<?xml version="1.0" encoding="UTF-8"?>
<Invoice>
	<InvoiceLineItem>
		<MonetaryAdjustment>
			<TaxAdjustment Code="Standard">
				<TaxPercent>
					<MeasurementValue>17.50</MeasurementValue>
				</TaxPercent>
				<TaxValue>1.00</TaxValue>
			</TaxAdjustment>
		</MonetaryAdjustment>
	</InvoiceLineItem>
	<InvoiceLineItem>
		<MonetaryAdjustment>
			<TaxAdjustment Code="Zero">
				<TaxPercent>
					<MeasurementValue>0.0</MeasurementValue>
				</TaxPercent>
				<TaxValue>2.00</TaxValue>
			</TaxAdjustment>
		</MonetaryAdjustment>
	</InvoiceLineItem>
	<InvoiceLineItem>
		<MonetaryAdjustment>
			<TaxAdjustment Code="Standard">
				<TaxPercent>
					<MeasurementValue>17.50</MeasurementValue>
				</TaxPercent>
				<TaxValue>3.00</TaxValue>
			</TaxAdjustment>
		</MonetaryAdjustment>
	</InvoiceLineItem>
</Invoice>

I am trying to create a summary XML document

<summary>
	<tax code="Standard" count="2" rate="17.50" tax="3.00"/>
	<tax code="Zero" count="1" rate="0.0" tax="0.00"/>
</summary>

My stylesheet is: -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" indent="yes"/>
	<xsl:key name="x"
match="Invoice/InvoiceLineItem/MonetaryAdjustment/TaxAdjustment"
use="@Code"/>
	<xsl:template match="/">
		<summary>
			<xsl:for-each
select="Invoice/InvoiceLineItem[generate-id(MonetaryAdjustment/TaxAdjustment
/@Code) = generate-id(key('x', @Code)[1])]">
				<xsl:variable name="N" select="MonetaryAdjustment/TaxAdjustment/@Code"/>
				<xsl:variable name="R"
select="MonetaryAdjustment/TaxAdjustment/TaxPercent/MeasurementValue"/>
				<xsl:variable name="C"
select="count(/Invoice/InvoiceLineItem/MonetaryAdjustment/TaxAdjustment[@Cod
e= $N])"/>
				<xsl:variable name="V"
select="sum(/Invoice/InvoiceLineItem/MonetaryAdjustment/TaxAdjustment[@Code=
$N]/TaxValue)"/>
				<tax code="{$N}" count="{$C}" rate="{$R}" tax="{$V}"/>
			</xsl:for-each>
		</summary>
	</xsl:template>
</xsl:stylesheet>


However, being a complete novice on the use of 'key' I achieve nothing,
other than a very nice xml prolog!.

Any help, preferably a modified XSL, and I would be eternally grateful.

Rob



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread