xsl-list
[Top] [All Lists]

RE: following-sibling in a sorted result tree?

2005-04-27 16:05:41
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(_at_)hotmail(_dot_)com] 
Sent: 27 April 2005 23:44
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] 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



--~------------------------------------------------------------------
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>
--~--



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