xsl-list
[Top] [All Lists]

Re: [xsl] build group of elements

2012-06-22 08:16:39
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/



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>