Re: [xsl] Grouping Using XSL

Subject: Re: [xsl] Grouping Using XSL
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Tue, 21 Jan 2003 02:58:22 +0100
So you want to group the data elements by their col 1 and col 2 values. You must concatenate those values for it:

<xsl:key name="data" match="data" use="concat(col[@id='1'], '::', col[@id='2'])"/>

Now you can apply-templates on group-starters:

<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="data[generate-id() = generate-id(key('data', concat(col[@id='1'], '::', col[@id='2'])))]" mode="unique"/>
</xsl:copy>
</xsl:template>


They are copied to the output and create a "container" col 3 element for the val elements. The next step is applying templates to all the data elements with the same concatenated string of col 1 and col2:

<xsl:template match="data" mode="unique">
<xsl:copy>
<xsl:copy-of select="col[@id='1' or @id='2']"/>
<col id="3">
<xsl:apply-templates select="key('data', concat(col[@id='1'], '::', col[@id='2'])" mode="col3"/>
</col>
</xsl:copy>
</xsl:template>


The val elements:

<xsl:template match="data" mode="col3">
  <val>
    <xsl:value-of select="col[@id='3']"/>
  </val>
</xsl:template>

The technique is called Muenchian Grouping and the default recommended page for it is http://www.jenitennison.com/xslt/grouping/muenchian.xml.

<xsl:for-each-group/> seems to be XSLT 2.0. The spec is only a working draft and not a recommendation. So there can still change some things. The Xalan group starts to implement it, another implementation is Saxon 7.x. But using the above with an XSLT 1.0 processor is more recommended at the moment I think.

Regards,

Joerg

Rigoberto wrote:
hi everybody!!!

I got some problems grouping some data from a xml doc. Actually the xml doc looks like this:

<data>
      <col id="1">1</col>
      <col id="2">1</col>
      <col id="3">hello</col>
</data>
<data>
      <col id="1">1</col>
      <col id="2">1</col>
      <col id="3">bye</col>
</data>
<data>
      <col id="1">1</col>
      <col id="2">1</col>
      <col id="3">oops</col>
</data>
<data>
      <col id="1">1</col>
      <col id="2">2</col>
      <col id="3">hello</col>
</data>

what i´m trying to do, is create an xml document grouping the document source using the col elements dinamically ( col[1], col[1] and col[2], col[2],etc). so, if i use col[1] and col[2] the results should looks like this:

<data>
      <col id="1">1</col>
      <col id="2">1</col>
      <col id="3">
                  <val>hello</val>
                  <val>bye</val>
                  <val>oops</val>
      </col>
</data>
<data>
      <col id="1">1</col>
      <col id="2">2</col>
      <col id="3">
                  <val>hello</val>
      </col>
</data>

i´m using xalan(xalan-j_2_4_1) processor so I THINK (maybe i´m wrong) that i can´t use the <xsl:for-each-group/> tag. can i use any other processor? what can i do? any ideas?
i´m using jdk1.3.1.


thanks.


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


Current Thread