How to combine XML elements using XSLT

Go To StackoverFlow.com

1

I have the following XML:

<root>
<section>
    <item name="a">
        <uuid>1</uuid>
    </item>
</section>
<section>
    <item name="b">
        <uuid>2</uuid>
    </item>
</section>
</root>

I would like to transform it into the following XML:

<root>
<section>
    <item name="a">
        <uuid>1</uuid>
    </item>
    <item name="b">
        <uuid>2</uuid>
    </item>
</section>
</root>

Thanks in advance.

Update.

A slightly different example contains additional elements and attributes.

Input:

<root age="1">
<description>some text</description>
<section>
    <item name="a">
        <uuid>1</uuid>
    </item>
</section>
<section>
    <item name="b">
        <uuid>2</uuid>
    </item>
</section>
</root>

I would like to transform it into:

<root age="1">
<description>some text</description>
<section>
    <item name="a">
        <uuid>1</uuid>
    </item>
    <item name="b">
        <uuid>2</uuid>
    </item>
</section>
</root>
2012-04-04 07:56
by Sasha Korman
What, exactly, is the question? What have you tried? What goes wrong - Paul Butcher 2012-04-04 08:13
@Paul Butcher I did not try anything since I'm not familiar with XSLT. The question is simple, how to get the desirable output given provided inpu - Sasha Korman 2012-04-04 08:19
Try something like Elance, guru.com, vWorker or oDes - Paul Butcher 2012-04-04 08:25


1

Following Xsl should work:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="section item"/>
    <xsl:template match="/root">
        <root>
            <section>
                <xsl:apply-templates select="section"/>
            </section>
        </root>
    </xsl:template>

    <xsl:template match="item">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

It gives:

<root>
   <section>
      <item name="a">
         <uuid>1</uuid>
      </item>
      <item name="b">
         <uuid>2</uuid>
      </item>
  </section>
</root>

Update:

For second example you can use following Xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="root item"/>

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

   <xsl:template match="description">
       <xsl:copy-of select="."/>
       <section>
           <xsl:apply-templates select="following-sibling::section/item"/>
       </section>
   </xsl:template>

   <xsl:template match="section" />

   <xsl:template match="item">
      <xsl:copy-of select="."/>
   </xsl:template>

</xsl:stylesheet>
2012-04-04 08:08
by Vitaliy
Thanks @Vitaliy it does the trick. However it does not work with my updated example. Could you please provide me with XSLT for the updated example - Sasha Korman 2012-04-04 08:41
Thanks a lot @Vitaliy. It works great - Sasha Korman 2012-04-04 09:14
Ads