This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: how to get new position() of a node in a sorted result tree?


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
original
> 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
(http://www.vbxml.com/snippetcentral/main.asp?view=viewsnippet&id=v20010310050532).

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.

Cheers,
Dimitre Novatchev.

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]