[plexippus-xpath-devel] do-node-set woes
Peter Stiernström
peter at stiernstrom.se
Tue May 26 11:42:00 UTC 2009
Thank you Ivan and David,
You are of course both correct that the error was in my xpath-fu and
not plexippus. I am thouroughly ashamed of this newbie question and
will make sure to inspect my expressions more vigirously before
posting again!
/Peter
On Tue, May 26, 2009 at 03:12:28PM +0400, Ivan Shvedunov wrote:
> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <https://mailman.common-lisp.net/pipermail/plexippus-xpath-devel/attachments/20090526/53cab106/attachment.sig>
More information about the plexippus-xpath-devel
mailing list