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

RE: [xsl] Regrouping XML Data


Subject: RE: [xsl] Regrouping XML Data
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Fri, 12 Jul 2002 09:39:45 +0100

Here's a novel solution using XSLT 2.0 grouping:

<row>
<xsl:for-each-group select="col/data"
group-by="count(preceding-sibling::*)">
  <col>
    <xsl:for-each select="current-group()">
       <xsl:copy-of select="."/>
    </xsl:for-each>
  </col>
</xsl:for-each-group>
</row>

You could of course solve it as a grouping problem in XSLT 1.0 as well.
But there are easier ways.

<xsl:variable name="row" select="."/>
<xsl:for-each select="col[1]/data">
  <xsl:variable name="pos" select="position()"/>
  <col>
    <xsl:for-each select="$row/col/data[position()=$pos]">
      <xsl:copy-of select="."/>
    </
  </
</

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Lotus Soda
> Sent: 12 July 2002 01:00
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Regrouping XML Data
> 
> 
> Dear Friends
> 
> I have the following XML data
>        <row>
>             <col>
>                 <data>
>                     Surveyor
>                 </data>
>                 <data>
>                     Excavation
>                 </data>
>                 <data>
>                     Plumber
>                 </data>
>             </col>
>             <col>
>                 <data>
>                     11/02/2000
>                 </data>
>                 <data>
>                     15/02/2000
>                 </data>
>                 <data>
>                     11/03/2000
>                 </data>
>             </col>
>        </row>
> 
> I am having difficulty transforming this into:
> 
>        <row>
>             <col>
>                 <data>
>                     Surveyor
>                 </data>
>                 <data>
>                     11/02/2000
>                 </data>
>             </col>
>             <col>
>                 <data>
>                     Excavation
>                 </data>
>                 <data>
>                     15/02/2000
>                 </data>
>             </col>
>             <col>
>                 <data>
>                     Plumber
>                 </data>
>                 <data>
>                     11/03/2000
>                 </data>
>             </col>
>        </row>
> 
> Each <row> has a fixed number of <col>
> Each <col> has one set of <data> (this set is variable)
> The <data> sets are equal for all <col> in each <row>
> 
> Any help is appreciated
> 
> 
> ('J') Lotus Soda
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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



Current Thread
Keywords