Re: [xml] xpath and the document order

  Hello Liam,

  thank you for comments.

On Fri, 06 May 2005 21:14:01 -0400
"Liam R. E. Quin" <liam holoweb net> wrote:

On Sat, 2005-05-07 at 01:29 +0400, Oleg A. Paraschenko wrote:

  as I understand, the function "xmlXPathEvalExpression" doesn't have
return nodes in the document order. So here are questions.

1) Could you show an example of XML and XPath, for which
   "xmlXPathEvalExpression" returns nodes not in the document order?

This most often happens in conjunction with the // operator,
with alternation (Xpath 2 only) and with the reverse axes such
as preceding-sibling.

  I tried different combinations, but failed and gave up. Maybe it's
because I don't believe that I can find an example. As far as I know,
a result nodeset shouldn't contain duplicates. One of the simplest
methods to find duplicates is to sort the nodeset, and I suspect that
"xmlXPathEvalExpression" does sort it.

  But maybe I just use an incorrect code to test. Here it is:

import libxml2

xml = '''
Do <emph>NOT</emph> touch the switch.
The computer will <emph>explode!</emph>
xpath = '/warning//text()'

doc = libxml2.parseDoc(xml)
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval(xpath)

for item in res:
  print "[[%s]]\n" % item.serialize()

2) What should be used for sorting a set of nodes:

"xmlXPathNodeSetSort" from "xpath.c" from libxml2, or
"xsltDocumentSortFunction" from "xsltutils.c" from libxslt?

Doesn't it depend whether you are sorting by document order or
by some other key?

  The both functions use the function "xmlXPathCmpNodes" as the
comparator, so the both functions sort by document order, not
by some other key.


Liam Quin, W3C XML Activity Lead,
Pictures from old books:
IRC (chat) programs:

Oleg Paraschenko  olpa@  XML news in Russian  Generative Programming, XML, TeX, Scheme

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]