Page 1 of 1

Shifting nodes in an XML using XSLT

Posted: Sun Nov 11, 2012 5:45 pm
by Hosch
Hi guys,

I have an XML with following structure

<root>
<header>
<line>
<line>
<line>
<subline>
<subline>
<subline>
<root>


how can I transform the above xml to the following structure using XSLT?
<root>
<line>
<subline>
<line>
<subline>
<line>
<subline>

each line is connected with the appropriate subline using a tag id

Thanks in advance for your support.

Re: Shifting nodes in an XML using XSLT

Posted: Tue Nov 20, 2012 11:50 am
by lorela
Hi,
You can use :

Code: Select all


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

<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates select="line | subline">
<xsl:sort select="id"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

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

</xsl:stylesheet>
Regards,
Lorela