[xsl] sorting into a tree structure

Subject: [xsl] sorting into a tree structure
From: jim mcgovern <jim.mcgovern2@xxxxxxxxxxxxxx>
Date: Tue, 6 Oct 2009 16:12:56 +0100
Hi All

I'm trying to find the most efficient way of sorting a flat xml file
into a tree structure.  My xslt so far is pretty simple and sorts the
XML into a HMTL tree structure <ul><li><ul>...etc.  But what I'm
having trouble with is the most efficient way of sorting the content.
My tree structure needs to be ordered using the order attribute in the
xml.  My xml is:-

<?xml version="1.0" encoding="utf-8" ?>
    <folders>
	<Item ID="1" ParentItem="" order="1" title="item1"/>
	<Item ID="2" ParentItem="1" order="2" title="item2"/>
	<Item ID="3" ParentItem="2" order="3" title="item3"/>
	<Item ID="4" ParentItem="2" order="2" title="item4"/>
	<Item ID="5" ParentItem="2" order="1" title="item5"/>
	<Item ID="6" ParentItem="1" order="1" title="item6"/>
	<Item ID="7" ParentItem="6" order="3" title="item7"/>
	<Item ID="8" ParentItem="6" order="2" title="item8"/>
	<Item ID="9" ParentItem="6" order="4" title="item9"/>
	<Item ID="10" ParentItem="6" order="1" title="item10"/>
    </folders>

And my xslt so far is pretty simple:-

<?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"/>
    <xsl:key name="val" match="Item" use="@ParentItem"/>
<xsl:template match="folders">
        <ul><xsl:apply-templates select="key('val', '')"/></ul>
    </xsl:template>
    <xsl:template match="Item">
        <xsl:choose>
            <xsl:when test="key('val', @ID)">
                <li><xsl:value-of select="@title"/></li>
                <ul><xsl:apply-templates select="key('val', @ID)"/></ul>
            </xsl:when>
            <xsl:otherwise>
                <li><xsl:value-of select="@title"/></li>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

I'd appreciate any pointers on how to implement an efficient sort
using the order attribute!

Many thanks

Jim

Current Thread