[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

sorting into a tree structure

Subject: sorting into a tree structure
From: jim mcgovern <jim.mcgovern2@xxxxxxxxxxxxxx>
Date: Tue, 6 Oct 2009 16:12:56 +0100
 sorting into a tree structure
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.