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

RE: following-sibling in a sorted result tree?

Subject: RE: following-sibling in a sorted result tree?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 28 Apr 2005 00:05:41 +0100
following sibling variable
I thought I was seeing double - exactly the same question twice in a day.

There's no way of referencing items in the current node list (the sequence
being processed by xsl:for-each) other than the current item. They aren't
necessarily siblings, and even if they are, the processing order is
different from the sibling order, because sorting doesn't modify the tree.
You can create a new tree containing copies of the nodes in a different
order (requiring the xx:node-set extension in 1.0). Or in 2.0, you can put
the sorted sequence in a variable:

<xsl:variable name="sorted-sequence" as="element()*">
  <xsl:perform-sort select="$input">
    <xsl:sort select="@key"/>
  </xsl:perform-sort>
</xsl:variable>

and then you can access items by position within the sorted sequence:

<xsl:for-each select="$sorted-sequence">
  <xsl:variable name="pos" select="position()"/>
  <xsl:variable name="prev" select="$sorted-sequence[$p - 1]"/>
  <xsl:variable name="next" select="$sorted-sequence[$p + 1]"/>

etc. This avoids the expensive operation of copying all the nodes and
building a new tree.

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: Peter Heald [mailto:healdp@xxxxxxxxxxx] 
> Sent: 27 April 2005 23:44
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  following-sibling in a sorted result tree?
> 
> Hi,
> 
> I have an xslt2 transform that outputs multiple html 
> documents, as well as 
> index files and hhc/hhk htmlHelp files.
> 
> Part of this is a sorted xsl:for-each loop that references 
> the previous and 
> next siblings (used to add next and previous page buttons in 
> the html).
> 
> I have found that the sibling instructions reference the 
> unsorted result 
> tree, not the sorted tree which is the current context. The 
> results match 
> the order of the original XML, not the sorted loop, therefore 
> the previous 
> and next buttons are not in sync with the index and content 
> pages, which are 
> also sorted. So I'm guessing that following-sibling, for 
> example, is not 
> using the sorted result tree at all. Is there a way around 
> this?  Do I 
> somehow need to create a parallel result tree?
> 
> Any thoughts would be much appreciated.  Below is a little 
> pseudo-code 
> demonstrating the problem.
> 
> <xsl:for-each select="ITEM">
>   <xsl:sort select="@NAME"/>
>     <xsl:result-document href="./{$something}.html" format="myhtml">
>         .....
>         <xsl:value-of 
> select="following-sibling::*[1]/@NAME"/> <!--next 
> sorted ITEM ? -->
>         ....
>     </xsl:result-document>
> </xsl:for-each>
> 
> Why would the value-of line above result the next sibling 
> according to the 
> original XML file order and not the sorted order?
> 
> Thanks!  Pete

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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