[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|