<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="parentid" match="item" use="@parent"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="root/item[@parent='0']"/>
</root>
</xsl:template>
<xsl:template match="item">
<xsl:element name="{name(.)}">
<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
<xsl:for-each select="key('parentid',@id)">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
|