xsl-list
[Top] [All Lists]

[xsl] Counting and double sorting on parameter

2007-02-08 05:24:36
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="*[(_at_)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="//*[(_at_)partNum and generate-id(.)=generate-id(key('CPUs', @partNum))]" >
           </xsl:apply-templates>
       </table>
   </xsl:template>
<xsl:template match="*[(_at_)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>




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>