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

Re: [xsl] build group of elements


Subject: Re: [xsl] build group of elements
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 24 Jun 2012 12:11:45 -0300

At 2012-06-24 15:41 +0100, henry human wrote:
sorry for my mistake..again the input xml data and the expecting output:
The xml data has many foo elements (1000).

Just by conjecture, I'm guessing you want <fooGroup> to be three groups of three. You don't explain the criteria, but that is the only "meaningful" interpretation I can see. Next time it would help if you could explain your requirements when including an example rather than make us guess by looking at your example.


If I've guessed correctly, this is done easiest with adjacent grouping.

I hope this helps.

. . . . . . . . . Ken

~/t/ftemp $ cat henry.xml
<?xml version="1.0" encoding="UTF-8"?>
 <xmldata>
 <ss> s </ss>
 <nn>n </nn>
  <foo>tt</foo>
   <foo>bb</foo>
   <foo>aa</foo>
    <foo>mm</foo>
       <foo>ll</foo>
       <foo>nn</foo>
        <foo>ff</foo>
         <foo>gg</foo>
      <foo>ii</foo>
  <hh>h</hh>
 </xmldata>
~/t/ftemp $ xslt2 henry.xml henry.xsl
<?xml version="1.0" encoding="UTF-8"?>
<fooGroup>
   <_1st_element>
      <_1st_element>tt</_1st_element>
      <_2nd_element>bb</_2nd_element>
      <_3rd_element>aa</_3rd_element>
   </_1st_element>
   <_2nd_element>
      <_1st_element>mm</_1st_element>
      <_2nd_element>ll</_2nd_element>
      <_3rd_element>nn</_3rd_element>
   </_2nd_element>
   <_3rd_element>
      <_1st_element>ff</_1st_element>
      <_2nd_element>gg</_2nd_element>
      <_3rd_element>ii</_3rd_element>
   </_3rd_element>
</fooGroup>~/t/ftemp $
~/t/ftemp $ cat henry.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:h="urn:X-henry" exclude-result-prefixes="h"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="xmldata">
  <xsl:for-each-group select="foo"
                      group-adjacent="(position()-1) idiv 9">
    <fooGroup>
      <xsl:for-each-group select="current-group()"
                          group-adjacent="(position()-1) idiv 3">
        <xsl:element name="_{h:ord(position())}_element">
          <xsl:for-each select="current-group()">
            <xsl:element name="_{h:ord(position())}_element">
              <xsl:copy-of select="node()"/>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each-group>
    </fooGroup>
  </xsl:for-each-group>
</xsl:template>

<xsl:function name="h:ord">
  <xsl:param name="pos"/>
  <xsl:number value="$pos" ordinal="1"/>
</xsl:function>

</xsl:stylesheet>
~/t/ftemp $


-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords