xsl-list
[Top] [All Lists]

Re: Sort question

2003-02-28 07:15:59
Thanks for your help and I understand what your getting at, but what if the XML wasn`t quite so simple, ie:

<CRUMBTRAILS>
        <CRUMBTRAIL>
                <ANCESTOR>
                        <NODEID>889</NODEID>
                        <NAME>Top</NAME>
                        <TREELEVEL>0</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>72</NODEID>
                        <NAME>Life</NAME>
                        <TREELEVEL>1</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>21</NODEID>
                        <NAME>Families</NAME>
                        <TREELEVEL>2</TREELEVEL>
                </ANCESTOR>
        </CRUMBTRAIL>
        <CRUMBTRAIL>
                <ANCESTOR>
                        <NODEID>889</NODEID>
                        <NAME>Top</NAME>
                        <TREELEVEL>0</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>73</NODEID>
                        <NAME>Hobbies</NAME>
                        <TREELEVEL>1</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>83</NODEID>
                        <NAME>Travel &amp; Transport</NAME>
                        <TREELEVEL>2</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>52</NODEID>
                        <NAME>Transport</NAME>
                        <TREELEVEL>3</TREELEVEL>
                </ANCESTOR>
        </CRUMBTRAIL>
        <CRUMBTRAIL>
                <ANCESTOR>
                        <NODEID>889</NODEID>
                        <NAME>Top</NAME>
                        <TREELEVEL>0</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>72</NODEID>
                        <NAME>Life</NAME>
                        <TREELEVEL>1</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>117</NODEID>
                        <NAME>Toys, Games &amp; Hobbies</NAME>
                        <TREELEVEL>2</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>797</NODEID>
                        <NAME>Toys</NAME>
                        <TREELEVEL>3</TREELEVEL>
                </ANCESTOR>
        </CRUMBTRAIL>
        <CRUMBTRAIL>
                <ANCESTOR>
                        <NODEID>889</NODEID>
                        <NAME>Top</NAME>
                        <TREELEVEL>0</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>72</NODEID>
                        <NAME>Life</NAME>
                        <TREELEVEL>1</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>117</NODEID>
                        <NAME>Toys, Games &amp; Hobbies</NAME>
                        <TREELEVEL>2</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>798</NODEID>
                        <NAME>Games</NAME>
                        <TREELEVEL>3</TREELEVEL>
                </ANCESTOR>
        </CRUMBTRAIL>
        <CRUMBTRAIL>
                <ANCESTOR>
                        <NODEID>889</NODEID>
                        <NAME>Top</NAME>
                        <TREELEVEL>0</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>72</NODEID>
                        <NAME>Life</NAME>
                        <TREELEVEL>1</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>117</NODEID>
                        <NAME>Toys, Games &amp; Hobbies</NAME>
                        <TREELEVEL>2</TREELEVEL>
                </ANCESTOR>
                <ANCESTOR>
                        <NODEID>800</NODEID>
                        <NAME>Computer Games</NAME>
                        <TREELEVEL>3</TREELEVEL>
                </ANCESTOR>
        </CRUMBTRAIL>
</CRUMBTRAILS>

With this I need to sort, in the same way only with <NAME> as my sort key. (Using your previous methodology it appears the NODEID and TREELEVEL is getting in the way with this tree)


Date: Thu, 27 Feb 2003 21:30:30 +0100
From: "Dimitre Novatchev" <dnovatchev(_at_)yahoo(_dot_)com>
Subject: [xsl] Re: Sort question



For your type of xml source document, yes, it is possible.

The following transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes"/>

 <xsl:variable name="vWhite" select="' &#10;&#13;'"/>
 <xsl:variable name="vLowest" select="'&#9;&#9;&#9;'"/>

 <xsl:template match="/">
   <sorted>
      <xsl:for-each select="/*/crumbtrail">
        <xsl:sort select="translate(., $vWhite, $vLowest)"/>

        <xsl:copy-of select="."/>
      </xsl:for-each>
   </sorted>
 </xsl:template>
</xsl:stylesheet>


when applied on this source.xml:

<root>
 <crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
   <crumb>football</crumb>
   <crumb>liverpool</crumb>
 </crumbtrail>
 <crumbtrail>
   <crumb>top3</crumb>
   <crumb>spo</crumb>
   <crumb>foot</crumb>
   <crumb>liverpool</crumb>
 </crumbtrail>
 <crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
 </crumbtrail>
 <crumbtrail>
   <crumb>top2</crumb>
   <crumb>sportX</crumb>
 </crumbtrail>
 <crumbtrail>
   <crumb>top2</crumb>
   <crumb>spo</crumb>
 </crumbtrail>
 <crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
   <crumb>cricket</crumb>
 </crumbtrail>
</root>

produces this result:

<sorted><crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
 </crumbtrail><crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
   <crumb>cricket</crumb>
 </crumbtrail><crumbtrail>
   <crumb>top</crumb>
   <crumb>sport</crumb>
   <crumb>football</crumb>
   <crumb>liverpool</crumb>
 </crumbtrail><crumbtrail>
   <crumb>top2</crumb>
   <crumb>spo</crumb>
 </crumbtrail><crumbtrail>
   <crumb>top2</crumb>
   <crumb>sportX</crumb>
 </crumbtrail><crumbtrail>
   <crumb>top3</crumb>
   <crumb>spo</crumb>
   <crumb>foot</crumb>
   <crumb>liverpool</crumb>
 </crumbtrail></sorted>


Of course, this is quite special case. In order for this to work two things
must be assured:

1. Whitespace nodes in the xml source must be preseved.

2. The "lowest character" for the given XSLT processor must be known in
advance, so that it can be used as the intended result of translating the
white-space.

3. The text-nodes children of crumb may contain only one (and the same) type
of whitespace character (e.g. only blanks are allowed).

Note that because the implementation of xsl:sort differs on different XSLT
processors (or even between different versions of the same XSLT procesor --
e.g. Xalan-J and Xalan-C), different results may be produced if the
xsl:variable "vLowest" is not set appropriately.

The above result is produced by: MSXML3/4, MS .Net xslTransform, Saxon
6.5.2, XalanC 1.3.

In order for XalanJ 2.4.1 to produce the same results the following should
be specified:

 <xsl:variable name="vLowest" select="000"/>


These limitations can be avoided if a generic-template/functional sort
implementation is used (or I guess in XSLT2 too).


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL







_________________________________________________________________
Stay in touch with MSN Messenger http://messenger.msn.co.uk


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>