[plexippus-xpath-devel] do-node-set woes

Ivan Shvedunov ivan4th at gmail.com
Tue May 26 11:12:28 UTC 2009


Hello,

2009/5/26 Peter Stiernström <peter at stiernstrom.se>:
> Hello xpath developers,
>
> I am trying to use plexippus-xpath and have hit a wall concerning the
> use of evaluate together with do-node-set. I am under the impression
> that the the second argument given to evaluate should be a document or
> a node. What I am trying to accomplish is to write a function that
> runs various xpath expressions on a subset of the document like so:
>
> (let ((doc (cxml:parse "<a><b><c><d>1</d></c><c><d>2</d></c><c><d>3</d></c></b></a>"
>                       (cxml-dom:make-dom-builder))))
>  (xpath:do-node-set (n (xpath:evaluate "//c" doc))
>    (format t "~d~%" (xpath:number-value (xpath:evaluate "//d" n)))))
>
> I would like to have this print:
> 1
> 2
> 3
>
> But instead it always prints:
> 1
> 1
> 1

It should be noted that //c in the last expression (same as
/descendant-or-self::node()/c) works
relative to the root node of the document and not the context node,
thus you get 1 every time.
Perhaps what you want is descendant::d as the last expression (.//d
will also work in this
concrete case though).

(let ((doc (cxml:parse
"<a><b><c><d>1</d></c><c><d>2</d></c><c><d>3</d></c></b></a>"
                       (cxml-dom:make-dom-builder))))
  (xpath:do-node-set (n (xpath:evaluate "//c" doc))
                     (format t "~d~%" (xpath:number-value
(xpath:evaluate "descendant::d" n)))))

(I'd write the FORMAT form as (format t "~d~%" (xpath:evaluate
"number(descendant::d)" n)) though).

> If I instead change my code to this:
>
> (let ((doc (cxml:parse "<a><b><c><d>1</d></c><c><d>2</d></c><c><d>3</d></c></b></a>"
>                       (cxml-dom:make-dom-builder))))
>  (xpath:do-node-set (n (xpath:evaluate "//c" doc))
>    (format t "~a~%" (dom:map-document (cxml:make-string-sink) n))))
>
> It prints (as expected):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <d>1</d>
> <?xml version="1.0" encoding="UTF-8"?>
> <d>2</d>
> <?xml version="1.0" encoding="UTF-8"?>
> <d>3</d>
>
> So my conclusion is that somehow my use of evaluate does not work
> according to my expectations, if those are valid or not is something I
> leave up to you to decide :)
>
> Now please strike me with your clue sticks!
>

Well, in this case the second expression is absent, so there's no problem :)

Ivan.




More information about the plexippus-xpath-devel mailing list