W3C home > Mailing lists > Public > www-xpath-comments@w3.org > January to March 2002

Re: Order of results

From: Jeni Tennison <jeni@jenitennison.com>
Date: Thu, 10 Jan 2002 18:14:04 +0000
Message-ID: <189293798790.20020110181404@jenitennison.com>
To: "James Swift" <Swift@altova.com>
CC: www-xpath-comments@w3.org
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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 3 October 2007 16:05:54 GMT