xsl-list
[Top] [All Lists]

two level grouping (+ put it in a loop)

2004-05-01 11:14:01
Hello,

I have a problem with a two level grouping question (i.e. with the
desired output)
There are n-number of elements (exemplary):
<elem>
        <number>4</number>
        <name>Thing</name>
        <related>related</related>
        <factor>1</factor>
        <count>3</count>
        <sum>0</sum>
</elem>

the desired outpu is a table like:

.   .   A   B   C   D
---------------------
1       x   x   x   x
---------------------
2       x   x   x   x
---------------------
3       x   x   x
---------------------
4                   x
---------------------
Sum:    x   x   x   x


I'm usig two key for this output.
 <xsl:key name="number" match="elem" use="number"/>
 <xsl:key name="related" match="elem" use="related"/>

<xsl:template match="data">
<table>
  <tr>
    <td colspan="2" />
    <xsl:for-each select="elem[generate-id(.) =
generate-id(key('number',number)[1])]">
      <td><xsl:value-of select="name"/></td>
    </xsl:for-each>
  </tr>
  <xsl:for-each select="elem[generate-id(.) =
generate-id(key('related',related)[1])]">
    <tr>
      <td><xsl:value-of select="related"/></td>
      <td><xsl:value-of select="factor"/></td>
      <xsl:for-each select="key('related', related)">
        <td><xsl:value-of select="count"/></td>
      </xsl:for-each>
    </tr>
  </xsl:for-each>
  <tr>
    <td colspan="2">Sum:</td>
    <xsl:for-each select="elem[generate-id(.) =
generate-id(key('number',number))]">
      <xsl:if test="key('number',number)[position() = last()]">
        <td><xsl:value-of select="sum"/></td><-- only the value of the
last() is OK here -->
      </xsl:if>
    </xsl:for-each>
  </tr>
</table>
</xsl:template>


The output is correct as long each <elem> in the same  key('number')
group has the same values in <related> in the key('related') group, but
not any more if one of them has also an other value.
For example the <td> for D4 is displayed in the place of A4.

Is there a possibility to get also all the empty <td>?

The second question is: is it possible to get all this in a recursive
template (a loop) to create a new <table> after  x-number of elem's,
becaus otherwiese the table would be excessive wide.

Thank you
Thomas


<Prev in Thread] Current Thread [Next in Thread>
  • two level grouping (+ put it in a loop), Thomas J. Sebestyen <=