Re: Clarification on node-set definition

Subject: Re: Clarification on node-set definition
From: Mike Brown <mike@xxxxxxxx>
Date: Sun, 26 Nov 2000 20:29:18 -0700 (MST)
Gavin Bong wrote:
> The XPath spec defines a node-set as:-
> - an unordered collection of nodes without duplicates.
> "Unordered" here basically means it hasn't been sorted on any
> properties of each node in the set. Is it correct to say that it is
> returned in document/sourcetree order ?

No, the order in which nodes are returned, if they are in fact provided
sequentially, is up to the implementation. The set itself does not have an
inherent order.

Once you have a node-set, you can then process the nodes in a particular
order, document order or reverse document order being the 2 most common
ones. This order is imposed upon the set at the time that it is processed,
though, not before. Example:

  <foo a1="one">bar</foo>

becomes the node tree
    |___element 'foo'
          |  \__attribute 'a1' w/value 'one'
          |___text 'bar'

If I use an XPath expression to select a node-set consisting of all nodes:

The set is { root, element 'foo', attribute 'a1', text 'bar' }. Because I am
writing the set linearly, I have to put the nodes in some order, so I
happened to choose document order. It would have been just as accurate to say
the set is { root, element 'foo', text 'bar', attribute 'a1' } because the
set doesn't have an order. It just contains those specific, nonrepeating

Now let's say the current node is the text node and I select its ancestors:

This will give me a node-set consisting of { root, element 'foo' }. There is
some significance to the fact that it is the ancestor axis. It means that if
I add a predicate to this expression I will get a context with the root node
at position 2 and the element 'foo' at position 1: reverse document
order. Likewise if I do a <xsl:for-each select="ancestor::*" I will process
the element node before the root. It doesn't mean that the set itself is in
reverse document order, though..  confused yet?

> e.g. if my xml doc was generated such that there is some sort order based on
> position in the sourcetree - then my app would consider it sorted.

I think you're probably creating an abstraction that will sufficiently
explain what's going on, 99% of the time. It's not accurate, though.

   - Mike
Mike J. Brown, software engineer at         My XML/XSL resources: in Denver, Colorado, USA 

 XSL-List info and archive:

Current Thread