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

RE: [xsl] Grouping


Subject: RE: [xsl] Grouping
From: Jarno.Elovirta@xxxxxxxxx
Date: Mon, 24 Mar 2003 10:55:42 +0200

Hi,

See FAQ entry <http://www.dpawson.co.uk/xsl/sect2/N4486.html#d981e94>.

> I have this xml:
> 
> <root>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> 	<child></child>
> </root>
> 
> and i have to write an xsl that gives this output:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
> 	<parent>
> 		<child></child>
> 		<child></child>
> 	</parent>
> 	<parent>
> 		<child></child>
> 		<child></child>
> 	</parent>	
> 	<parent>
> 		<child></child>
> 		<child></child>
> 	</parent>	
> 	<parent>
> 		<child></child>
> 		<child></child>
> 	</parent>
> 	<parent>
> 		<child></child>
> 		<child></child>
> 	</parent>
> </root>
> 
> that is a grouping of 2 child, but this number is not a 
> constant, instead it is a parameter.

<xsl:param name="n" select="2" />

<xsl:template match="root">
  <xsl:copy>
    <xsl:for-each select="child[position() mod $n = 1]">
      <parent>
        <xsl:copy-of select=". | following-sibling::child[position() &lt; $n]" />
      </parent>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

Cheers,

Jarno - L´âme Immortelle: Tiefster Winter

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



Current Thread
  • [xsl] Grouping
    • Tevoi Andrea - Mon, 24 Mar 2003 09:27:35 +0100
      • <Possible follow-ups>
      • Jarno . Elovirta - Mon, 24 Mar 2003 10:55:42 +0200 <=
Keywords
xsl