Re: Order of results

Hi James,

> I have a question about the order of an XPath's results (assuming a
> node set result)

Node sets are, by definition, unordered (and a node set always
contains distinct nodes). The nodes in node sets are usually
*processed* in document order (unless you sort them with xsl:sort).

In XPath 2.0, node sequences that are generated with a path expression
contain the distinct nodes selected by the path expression, always in
document order.

You can, however, generate node sequences in other ways, for example
with the for expression.  So while:

  /descendant-or-self::node()/child::*

would return all the elements in the document in document order,

  for $n in /descendant-or-self::node()
  return child::*

would contain the document element, followed by the document element's
children, followed by the children of the first child of the document
element, followed by the children of the first child of the first
child of the document element and so on. From your example:

  (a,
   b,
   c, c, c,
   d, e, f,
   d, e, f,
   d, e, f)

In some circumstances this can lead to duplicate nodes. For example,
if you did:

  for $n in /descendant-or-self::node()
  return descendant::*

You'd get:

  (a, b, c, d, e, f, c, d, e, f, c, d, e, f,
   b, c, d, e, f, c, d, e, f, c, d, e, f,
   c, d, e, f, c, d, e, f, c, d, e, f,
   d, e, f,
   d, e, f,
   d, e, f)

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Thursday, 10 January 2002 13:14:05 UTC