1
votes

I'm a bit new to XSLT, but I'm trying to merge child elements of sibling nodes into a single node containing all the data.

So for example, my data structure looks something like this:

<root>
    <item>
        <child index="0">
            <data>fooA</data>
            <data>fooB</data>
            <data>fooC</data>
            <data>fooD</data>
        </child>
        <child index="1">
            <data>foo1</data>
            <data>foo2</data>
            <data>foo3</data>
            <data>foo4</data>
        </child>
    </item>
    <item>
        <child index="0">
            <data>barE</data>
            <data>barF</data>
            <data>barG</data>
            <data>barH</data>
        </child>
        <child index="1">
            <data>bar5</data>
            <data>bar6</data>
            <data>bar7</data>
            <data>bar8</data>
        </child>
    </item>
</root>

and I want to transform it like this:

<root>
    <item>
        <child index="0">
            <data>fooA</data>
            <data>fooB</data>
            <data>fooC</data>
            <data>fooD</data>
            <data>barE</data>
            <data>barF</data>
            <data>barG</data>
            <data>barH</data>
        </child>
        <child index="1">
            <data>foo1</data>
            <data>foo2</data>
            <data>foo3</data>
            <data>foo4</data>
            <data>bar5</data>
            <data>bar6</data>
            <data>bar7</data>
            <data>bar8</data>
        </child>
    </item>
</root>

So I think I need to somehow group by index and then just select each element, but I'm not quite sure how to do that.

Thanks in advance...

3
Would be hard to do universally. You want to group by the index attribute or by being n-th child of two elements?Nux

3 Answers

2
votes

This might work or at least give you a start:

<xsl:for-each select="//item[1]/child">
    <xsl:element name="child">
        <xsl:attribute name="index"><xsl:value-of select="@index" /></xsl:attribute>
        <xsl:for-each select="//item/child[@index=@index]/*">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:element>
</xsl:for-each>

But XSL in general is not the best suited for parametrized operations. If interal for will not work you might also try xsl:call-template run with param.

Also note that xsl:copy doesn't work well with too many XSL engines, so you might need to use something else (e.g. use apply-templates and write templates for your elements).

0
votes

I ended up using a Muenchian Grouping which worked well for me.

0
votes

Example of Muenchian Grouping...

XML Input

<root>
    <item>
        <child index="0">
            <data>fooA</data>
            <data>fooB</data>
            <data>fooC</data>
            <data>fooD</data>
        </child>
        <child index="1">
            <data>foo1</data>
            <data>foo2</data>
            <data>foo3</data>
            <data>foo4</data>
        </child>
    </item>
    <item>
        <child index="0">
            <data>barE</data>
            <data>barF</data>
            <data>barG</data>
            <data>barH</data>
        </child>
        <child index="1">
            <data>bar5</data>
            <data>bar6</data>
            <data>bar7</data>
            <data>bar8</data>
        </child>
    </item>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="childByIndex" match="child" use="@index"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/root">
    <root>
      <item>
        <xsl:for-each select="item/child[count(.|key('childByIndex',@index)[1])=1]">
          <child>
            <xsl:apply-templates select="@*|key('childByIndex',@index)/*"/>
          </child>
        </xsl:for-each>
      </item>
    </root>
  </xsl:template>

</xsl:stylesheet>

XML Output

<root>
   <item>
      <child index="0">
         <data>fooA</data>
         <data>fooB</data>
         <data>fooC</data>
         <data>fooD</data>
         <data>barE</data>
         <data>barF</data>
         <data>barG</data>
         <data>barH</data>
      </child>
      <child index="1">
         <data>foo1</data>
         <data>foo2</data>
         <data>foo3</data>
         <data>foo4</data>
         <data>bar5</data>
         <data>bar6</data>
         <data>bar7</data>
         <data>bar8</data>
      </child>
   </item>
</root>

Working Example