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

Re: [xsl] Problem with count iterate values


Subject: Re: [xsl] Problem with count iterate values
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Sat, 19 Sep 2009 19:46:07 +0200

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/


Current Thread