This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How to select a numbered group of elements
- From: "Joerg Heinicke" <joerg dot heinicke at gmx dot de>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 19 Feb 2002 21:26:12 +0100
- Subject: Re: [xsl] How to select a numbered group of elements
- References: <80256B65.005A886C.00@lonhm0004.arthurandersen.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Philip,
Carmelo gave the right advise. With position() mod 3 you select every third
tx, so you now where to do the sums. Before you have to print every tx,
which happens in the template matching tx without mode.
Regards,
Joerg
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="payment">
<table border="1">
<xsl:apply-templates select="tx[position() mod 3 = 1]"
mode="checksum"/>
</table>
</xsl:template>
<xsl:template match="tx" mode="checksum">
<xsl:variable name="tx" select=" . | following-sibling::tx[position()
< 3]"/>
<xsl:apply-templates select="$tx"/>
<tr>
<td colspan="2">Checksum: </td>
<td><xsl:value-of select="sum($tx/amt)"/></td>
</tr>
</xsl:template>
<xsl:template match="tx">
<tr>
<td><xsl:value-of select="paymentId"/></td>
<td><xsl:value-of select="payType"/></td>
<td><xsl:value-of select="amt"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
> I have an xml file like (there can be any number of <tx> elements)
>
> <payment>
>
<tx><paymentId>123456</paymentId><payType>EURO</payType><amt>10.00</amt></tx
>
>
<tx><paymentId>123457</paymentId><payType>EURO</payType><amt>20.00</amt></tx
>
>
<tx><paymentId>123458</paymentId><payType>EURO</payType><amt>30.00</amt></tx
>
>
<tx><paymentId>123459</paymentId><payType>EURO</payType><amt>40.00</amt></tx
>
>
<tx><paymentId>123460</paymentId><payType>EURO</payType><amt>50.00</amt></tx
>
> </payment>
>
> which I need to group in sets of 3 so that I can output a checksum line
every
> three lines of the <amt> tags. I also need to do this for the last line as
well
>
> eg
> Header Text
> 123456 EURO 10.00
> 123457 EURO 20.00
> 123458 EURO 30.00
> Checksum 60.00
> 123459 EURO 40.00
> 123460 EURO 50.00
> Checksum 90.00
> Footer text
>
> Is this possible with XSLT
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list