Re: Using node-sets in reverse document order?

Subject: Re: Using node-sets in reverse document order?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 10 Jul 1999 11:23:10 -0700
At 99/07/10 13:44 -0400, Tony Graham wrote:
>The ancestor, ancestor-or-self, preceding, and preceding-sibling axes
>contain nodes in reverse document order.  How is it possible to
>iterate over the nodes in that order when xsl:for-each processes nodes
>in document order and xsl:sort sorts on string value, not document
>order?

By "walking" the source tree along the axis in the direction of the first
member of the context node list, rather than dealing with the context node
list as a whole.

>How can I do something like the following that will order the selected
>nodes in reverse document order?
>
>   <xsl:for-each select="from-ancestors(node())">
>     <p>Element: <xsl:value-of select="qname()"/></p>
>   </xsl:for-each>

My example below illustrates how.

I hope this helps.

.......... Ken



T:\FTEMP>type test.xml
<?xml version="1.0"?>
<test>
<a>This is a
<b>This is b
<c>This is c
<d>This is d
<e>This is e</e>
</d>
</c>
</b>
</a>
</test>
T:\FTEMP>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<xsl:template match="/">                    <!--root rule-->
    <xsl:apply-templates mode="start" select="//e"/>
</xsl:template>                          

<xsl:template mode="start" match="*">
    <xsl:text>&#xa;Document Order:</xsl:text>
    <xsl:for-each select="from-ancestors(*)">
      <xsl:text>&#xa;Element: </xsl:text>
      <xsl:value-of select="qname()"/>
    </xsl:for-each>

    <xsl:text>&#xa;&#xa;Axis Order:</xsl:text>
    <xsl:apply-templates select="from-ancestors(*[1])"/>
</xsl:template>                                                

<xsl:template match="*">
    <xsl:text>&#xa;Element: </xsl:text>
    <xsl:value-of select="qname()"/>
    <xsl:apply-templates select="from-ancestors(*[1])"/>
</xsl:template>

</xsl:stylesheet>

T:\FTEMP>call xsl test.xml test.xsl test.txt
T:\FTEMP>type test.txt

Document Order:
Element: test
Element: a
Element: b
Element: c
Element: d

Axis Order:
Element: d
Element: c
Element: b
Element: a
Element: test
T:\FTEMP>


--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Publications:   Introduction to XSLT (3rd Edition) ISBN 1-894049-00-4
Next instructor-led training:   MS'99 1999-08-16  MT'99 1999-12-05/06


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


Current Thread