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

Re: [xsl] build group of elements


Subject: Re: [xsl] build group of elements
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Fri, 22 Jun 2012 15:16:17 +0200

henry human wrote:
The input file was just a sample to show the xml data has more foo elements.
<myxmlfile>
<ss></ss>...<foo></foo>.................<foo></foo> </myxmlfile>
//presume there are 100 foo elements in the xml file
My sample output which is created througth  xslt
  has groups of foo elment. Each group consists of three foo elements.
  (each three foo elments build a group). example
  <fooGroup>= foo[1], foo[2]. foo[3]
  <fooGroup>= foo[4], foo[5]. foo[6]
  <fooGroup>= foo[7], foo[8]. foo[9]

Do you use XSLT 2.0 or 1.0? With 2.0 you could do e.g. <xsl:param name="size" select="3"/>

<xsl:template match="xmlfile">
<xsl:for-each-group select="foo" group-by="(position() - 1) idiv $size">
<fooGroup>
<xsl:apply-templates select="current-group()"/>
</fooGroup>
</xsl:for-each-group>
</xsl:template>


  <xsl:template match="foo">
    <xsl:element name="foo{position()}">
       <xsl:copy-of select="node()"/>
    </xsl:template>
  </xsl:template>




--


	Martin Honnen --- MVP Data Platform Development
	http://msmvps.com/blogs/martin_honnen/


Current Thread
Keywords