Re: [xsl] sorting into a tree structure

Subject: Re: [xsl] sorting into a tree structure
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Tue, 06 Oct 2009 17:21:02 +0200
jim mcgovern wrote:
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!

Do you want to sort the li children of each ul? Then the following change to your stylesheet should do:


    <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)">
                    <xsl:sort select="order" data-type="number"/>
                  </xsl:apply-templates>
                </ul>
            </xsl:when>
            <xsl:otherwise>
                <li><xsl:value-of select="@title"/></li>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>



--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread