Jez Higgins

Freelance software generalist
software created
extended or repaired


Older posts are available in the archive or through tags.

Feed

Follow me on Twitter
My code on GitHub

Contact
About

Thursday 02 June 2005 Arabica: XPath: document order

I've also sorted out a document order issue, so that string(ns) returns the right thing. Most functions which operate on a node-set want the node that comes first in document order. However, until now the node-set returned by reverse axes where, unsurprisingly, in reverse order so they gave the wrong result. Node-sets now keep track of whether they are forward or reverse, so they do the right thing at the right time. Several other XPath implementations get this wrong, btw. For instance XPath Explorer have a play with.

Here's what I mean

<document>
  <chapter>one</chapter>
  <chapter>two</chapter>
  <chapter>three</chapter>
  <chapter>four</chapter>
  <chapter>five</chapter>
</document>
string(/document/*) = "one"
string(/document/*[last()]) = "five"
string(/document/chapter[5]/preceding-sibling::*[1] = "four"
string(/document/chapter[5]/preceding-sibling::*[last()]) = "one"
string(/document/chapter[5]/preceding-sibling::*) = "one"
That last expression is the most interesting. It selects all the chapter elements before the last one in reverse document order (as we can see from the previous two expressions), but the string() function returns the value of the node which comes first in document order.

One remaining issue here - my implementation of the union operator may not (depending on what you're joining together) return the node-set in any kind of order, in which case this all breaks down.


Tagged code, arabica, xml, and c++


Jez Higgins

Freelance software generalist
software created
extended or repaired

Older posts are available in the archive or through tags.

Feed

Follow me on Twitter
My code on GitHub

Contact
About