xsl-list
[Top] [All Lists]

Re: [xsl] Problem with count iterate values

2009-09-19 15:46:14
Hello
I do not know the difference, but it seems that I can not use this header:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:exsl="http://exslt.org/common";
exclude-result-prefixes="exsl"
version="1.0">
Tag form template David Carlisle:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               xmlns:exslt="http://exslt.org/common";
               version="1.0">
was accepted but this one in my system display nothing. His template
works correctly.  My system don't support exslt well. I wonder if it
is a question of this declaration or  function of exsl which you used.
I don't know which may be reason of this problem.

Static columns are OK. I lost many hours trying to make recursive
columns, and it is quite good idea for this problem.
Kind regards.

2009/9/19 Martin Honnen <Martin(_dot_)Honnen(_at_)gmx(_dot_)de>:
J23 wrote:

xml input:
<?xml version="1.0"?>
<?xml-stylesheet href="xsl.xsl" type="text/xsl" ?>
<elements>
       <Row Nr="1">
               <name>Mark</name>
               <value>1</value>
               <code>22</code>
       </Row>
       <Row Nr="2">
               <name>Mark</name>
               <value>1</value>
               <code>1</code>
       </Row>

       <Row Nr="3">
               <name>Paul</name>
               <value>2</value>
               <code>2</code>
       </Row>
       <Row Nr="4">
               <name>Mark</name>
               <value>1</value>
               <code>2</code>
       </Row>
       <Row Nr="5">
               <name>Peter</name>
               <value>44</value>
               <code>2</code>
       </Row>
       <Row Nr="6">
               <name>Peter</name>
               <value>1</value>
               <code>0</code>
       </Row>
       <Row Nr="7">
               <name>Paul</name>
               <value>11</value>
               <code>1</code>
       </Row>
       <Row Nr="8">
               <name>Peter</name>
               <value>11</value>
               <code>1</code>
       </Row>
       <Row Nr="9">
               <name>Mark</name>
               <value>13</value>
               <code>0</code>
       </Row>
       <Row Nr="10">
               <name>Peter</name>
               <value>13</value>
               <code>1</code>
       </Row>
       <Row Nr="11">
               <name>Paul</name>
               <value>14</value>
               <code>1</code>
       </Row>
</elements>

The following stylesheet

<xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:exsl="http://exslt.org/common";
 exclude-result-prefixes="exsl"
 version="1.0">

 <xsl:output method="html" indent="yes"/>

 <xsl:key name="k1" match="Row[not(code = 0)]" use="name"/>
 <xsl:key name="k2" match="Row[not(code = 0)]" use="concat(name, '|',
value)"/>
 <xsl:key name="k3" match="data" use="concat(../@key, '|', @count)"/>

 <xsl:variable name="cols-rtf">
   <td>1</td>
   <td>2</td>
   <td>3</td>
   <td>4</td>
   <td>5</td>
 </xsl:variable>

 <xsl:variable name="cols" select="exsl:node-set($cols-rtf)/td"/>

 <xsl:template match="/">
   <html>
     <head>
       <title>Example</title>
     </head>
     <body>
       <xsl:variable name="groups-rtf">
         <xsl:for-each select="elements/Row[not(code = 0)][generate-id() =
generate-id(key('k1', name)[1])]">
           <group key="{name}">
             <xsl:for-each select="key('k1', name)[generate-id() =
generate-id(key('k2', concat(name, '|', value))[1])]">
               <xsl:sort select="count(key('k2', concat(name, '|', value)))"
data-type="number"/>
               <data value="{value}" count="{count(key('k2', concat(name,
'|', value)))}"/>
             </xsl:for-each>
           </group>
         </xsl:for-each>
       </xsl:variable>
       <xsl:variable name="groups"
select="exsl:node-set($groups-rtf)/group"/>
       <table border="1">
         <thead>
           <tr>
             <th>Name</th>
             <th colspan="{count($cols)}">Values repeated n time(s)</th>
           </tr>
           <tr>
             <th></th>
             <xsl:copy-of select="$cols"/>
           </tr>
         </thead>
         <tbody>
           <xsl:for-each select="$groups">
             <xsl:variable name="cg" select="."/>
             <tr>
               <td><xsl:value-of select="@key"/></td>
               <xsl:for-each select="$cols">
                 <xsl:variable name="col" select="."/>
                 <td>
                   <xsl:for-each select="$cg">
                     <xsl:variable name="dg" select="key('k3', concat(@key,
'|', $col))"/>
                     <xsl:choose>
                       <xsl:when test="$dg">
                         <xsl:value-of select="count($dg)"/>
                         <xsl:text> (values </xsl:text>
                         <xsl:for-each select="$dg">
                           <xsl:value-of select="@value"/>
                           <xsl:if test="position() != last()">
                             <xsl:text>, </xsl:text>
                           </xsl:if>
                         </xsl:for-each>
                         <xsl:text>)</xsl:text>
                       </xsl:when>
                       <xsl:otherwise>0</xsl:otherwise>
                     </xsl:choose>
                   </xsl:for-each>
                 </td>
               </xsl:for-each>
             </tr>
           </xsl:for-each>
         </tbody>
       </table>
     </body>
   </html>
 </xsl:template>

</xsl:stylesheet>

produces the following result:

<html>
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

     <title>Example</title>
  </head>
  <body>
     <table border="1">
        <thead>
           <tr>
              <th>Name</th>
              <th colspan="5">Values repeated n time(s)</th>
           </tr>
           <tr>
              <th></th>
              <td>1</td>
              <td>2</td>
              <td>3</td>
              <td>4</td>
              <td>5</td>
           </tr>
        </thead>
        <tbody>
           <tr>
              <td>Mark</td>
              <td>0</td>
              <td>0</td>
              <td>1 (values 1)</td>
              <td>0</td>
              <td>0</td>
           </tr>
           <tr>
              <td>Paul</td>
              <td>3 (values 2, 11, 14)</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
           </tr>
           <tr>
              <td>Peter</td>
              <td>3 (values 44, 11, 13)</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
              <td>0</td>
           </tr>
        </tbody>
     </table>
  </body>
</html>

I hope that reflects what you want to achieve.
I cheated however by putting five columns statically in the stylesheet. If
you expect more repetitions than five then you would either need to edit the
stylesheet to add as much as you expect or you will need to implement some
column generation first based on the maximum count the stylesheet finds.



--

       Martin Honnen
       http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
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>
--~--





-- 
Pozdrawiam
Jacek Dunia

--~------------------------------------------------------------------
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>
--~--