Re: [xsl] Extracting the grouping from a flat structure

Subject: Re: [xsl] Extracting the grouping from a flat structure
From: Kevin Jones <kjones@xxxxxxxxxxx>
Date: Tue, 7 Dec 2004 09:42:03 +0000
On Monday 06 December 2004 22:52, Peter Wyngaard wrote:
> Hi Kev --
>
> Thanks for your reply.  Could you post sample code for
> your two-pass idea?  I'm really new at XSL and I don't
> think I follow your idea.

Sure, it is rather longer than your original but it might 
help. I have used the non-standard node-set() function in 
here, if you are not familiar with this have a read of 
http://www.xml.com/pub/a/2003/07/16/nodeset.html. I have 
also not tried to reproduce your exact formatting just the 
general structure you needed.

Kev

<xsl:template match='table'>
 <xsl:variable name="postable">
  <index>
   <xsl:for-each select="tr">
    <xsl:if test="th">
     <pos><xsl:value-of select="position()"/></pos>
    </xsl:if>
   </xsl:for-each>
  </index>
 </xsl:variable>

 <xsl:call-template name="genxml">
  <xsl:with-param name="pos"
 	select="node-set($postable)/index/pos"/>
  <xsl:with-param name="nodes" select="tr"/>
 </xsl:call-template>
</xsl:template>

<xsl:template name="genxml">
 <xsl:param name="pos"/>
 <xsl:param name="nodes"/>

 <xsl:choose>
  <xsl:when test="count($pos)=0"/>
  <xsl:when test="count($pos)=1">
   <header>
    <xsl:copy-of select="$nodes[position()=number($pos)]"/>
    <xsl:for-each select="$nodes[position()>number($pos)]">
     <row><xsl:copy-of select="."/></row>
    </xsl:for-each>
   </header>
  </xsl:when>
  <xsl:otherwise>
  <header>
   <xsl:copy-of select="$nodes[position()=number($pos)]"/>
   <xsl:for-each select="$nodes[position()>number($pos[1])
           and position()&lt;number($pos[2])]">
    <row>
     <xsl:copy-of select="."/>
    </row>
   </xsl:for-each>
  </header>
  <xsl:call-template name="genxml">
   <xsl:with-param name="pos" select="$pos[position()>1]"/>
   <xsl:with-param name="nodes" select="$nodes"/>
  </xsl:call-template>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

Current Thread