xsl-list
[Top] [All Lists]

Re: [xsl] sorting into a tree structure

2009-10-06 11:58:32
Hi Martin

Thanks for the quick reply.  Both ULs and LIs need to be sorted by the
order attribute.  For example, the output I'm looking to achieve would
be:-

<ul>
        <li>item1</li>
        <ul>
                <li>item6</li>
                <ul>
                        <li>
                                <a href="#">item10</a>
                        </li>
                        <li>
                                <a href="#">item8</a>
                        </li>
                        <li>
                                <a href="#">item7</a>
                        </li>
                        <li>
                                <a href="#">item9</a>
                        </li>
                </ul>
                <li>item2</li>
                <ul>
                        <li>
                                <a href="#">item5</a>
                        </li>
                        <li>
                                <a href="#">item4</a>
                        </li>
                        <li>
                                <a href="#">item3</a>
                        </li>
                </ul>
        </ul>
</ul>

I've tried your suggestion but it didn't seem to affect the order
unfortunately. :(

Thanks

Jim



On Tue, Oct 6, 2009 at 4:21 PM, Martin Honnen 
<Martin(_dot_)Honnen(_at_)gmx(_dot_)de> wrote:
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/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--