[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Counting and double sorting on parameter


Subject: Re: [xsl] Counting and double sorting on parameter
From: Xavier Outhier <xavier.outhier@xxxxxxxxxxx>
Date: Thu, 08 Feb 2007 13:46:22 +0100

Ahoj Tomas,

Kolacm Toma9 wrote:
Maybe this way:

<xsl:template match="tiptop">
    <table>
        <tr><th>partNum</th><th>Qty</th></tr>
        <xsl:apply-templates select="//*[@partNum and generate-id(.)=generate-id(key('CPUs', @partNum))]" >

             <xsl:sort
                 select="count(key('CPUs', @partNum))"
                 data-type="number"
                 order="descending"/>

</xsl:apply-templates>
</table>
</xsl:template>
This gives:
partNum      Qty
1234     2
234     2
934     2
54     1
89     1
94     1
12     1
26     1

This one below give the complete double sort I wanted:
<xsl:apply-templates select="//*[@partNum and generate-id(.)=generate-id(key('CPUs', @partNum))]" >
<xsl:sort
select="count(key('CPUs', @partNum))"
data-type="number"
order="descending"/>
<xsl:sort
select="@partNum"
order="ascending"/>
</xsl:apply-templates>


Dekuju, you saved me hours. :)

Xavier.
Tomas

-----Original Message-----
From: Xavier Outhier [mailto:xavier.outhier@xxxxxxxxxxx]
Sent: Thursday, February 08, 2007 1:24 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Counting and double sorting on parameter

Hi all,

I would like to make a summary of a file by counting the occurence
of the value of a parameter of a certain element. I've used
6. Dynamically counting an attribute from
http://www.dpawson.co.uk/xsl/sect2/N2018.html.
I've problem to sort the result. Can someone have an idea?
What are the change I have to do in the XSLT?

I'm using Saxon.

Below are my data
1) the XML input
2) The expected output:
3) the current output
4) the complete XSL

Thanx,

Xavier.

1) the XML input
<?xml version="1.0" encoding="UTF-8"?>
<tiptop>
    <qwerty>
        <CPU partNum="1234"/>
        <CPU partNum="234"/>
        <CPU partNum="234"/>
        <CPU partNum="1234"/>
        <CPU partNum="54"/>
    </qwerty>
    <qwerty>
        <CPU partNum="89"/>
        <CPU partNum="94"/>
        <CPU partNum="934"/>
        <CPU partNum="934"/>
    </qwerty>
    <CPU partNum="12"/>
    <CPU partNum="26"/>
</tiptop>

2) The expected output:

partNum      Qty
1234     2
234     2
934     2
12     1
26     1
54     1
89     1
94     1

3) the current output

partNum      Qty
1234     2
234     2
54     1
89     1
94     1
934     2
12     1
26     1

4) the complete XSL

<xsl:stylesheet    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
    <xsl:output method="html" indent="yes"/>

<xsl:key name="CPUs" match="*[@partNum]" use="@partNum" />

    <xsl:template match="/">
        <html><head></head>
            <body>
                <xsl:apply-templates select="*"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="tiptop">
        <table>
            <tr><th>partNum</th><th>Qty</th></tr>
            <xsl:apply-templates select="//*[@partNum and
generate-id(.)=generate-id(key('CPUs', @partNum))]" >
            </xsl:apply-templates>
        </table>
    </xsl:template>

<xsl:template match="*[@partNum]">
<tr>
<!-- first column is the value of the partNum attribute -->
<td><xsl:value-of select="@partNum" /></td>
<!-- second column is the number of parts with that partNum --
<td><xsl:value-of select="count(key('CPUs', @partNum))"
/></td>
</tr>
</xsl:template>


</xsl:stylesheet>


[...]


Current Thread
Keywords
xml