user902870
user902870

Reputation: 19

XSLT/XSL recursive nested elements

I need create a recursive transformation in XSL,
input xml

<root><foo1 /><foo2 /><foo3 /></root>

output

<root> 
 <foo1>
   <foo2>
     <foo3>
     </foo3>
    </foo2>
  <foo1> 
</root>

Thanks a lot for any help...

Upvotes: 1

Views: 102

Answers (1)

hr_117
hr_117

Reputation: 9627

Try something like this:

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="/root">
        <xsl:copy>
            <xsl:apply-templates select="*[1]" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="following-sibling::*[1]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Which will generate the following output:

<?xml version="1.0"?>
<root>
  <foo1>
    <foo2>
      <foo3/>
    </foo2>
  </foo1>
</root>

Upvotes: 3

Related Questions