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

Re: how to get new position() of a node in a sorted re

Subject: Re: how to get new position() of a node in a sorted result tree?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 2 Aug 2001 01:44:42 -0700 (PDT)
xsl sorted position
David Li wrote:

>     Anyone knows how to get the new node position() of a sorted result tree?
>     Specifically, I have the following XSLT code:
>         <xsl:template match="/">
>                 <xsl:apply-templates select="//item">
>                         <xsl:sort data-type="number" order="descending" 
> select="@date" />
>                 </xsl:apply-templates>
>                 </font>
>         </xsl:template>
>         <xsl:template match="article">
>                 <!-- process only the first 100 -->
>                 <xsl:if test="position() &lt; 100">
>                 ......
>                 </xsl:if>
>         </xsl:template>
>     When I call the position() function, it returns the position ID in the
> tree not the position ID in the new sorted tree. How to get the new position in a
> sorted tree?

Hi David,

In the code you've shown us you're not instantiating the template which matches
"article" via an xsl:apply-templates selecting "article" elements and followed by an
appropriate xsl:sort.

As I do not have all your code, I guess that you're not instantiating your template
matching "article" in this way at all. 

This explains the observed behaviour and it is the usual one when a template is
instantiated through an xsl:apply-templates without any xsl:sort child.

Another ***important*** note is that using xsl:sort to only access the first 100
sorted nodes may be grossly inefficient. The time complexity of sorting N nodes is 
O(N x Log2(N)).

You can use a partial sort algorithm, like the one implemented by the sort() generic
template (http://www.vbxml.com/downloads/default.asp?id=v2001611171627) or by the
partial sort snippet

The time complexity of the partial sort algorithm as implemented above is 
O(N x Log2(k)), where k is the number of nodes that actually have to be sorted (in
your case 100). 

It is Log2(N) div Log2(k) times faster that applying a full sort. 

Hope this helped.

Dimitre Novatchev.

Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger

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

Current Thread


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.
First Name
Last Name
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.