multiple XPath evaluations on the same document

View: New views
2 Messages — Rating Filter:   Alert me  

multiple XPath evaluations on the same document

by piotreks :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I have a question regarding multiple XPath evaluations on the same
document, specifically:
Assuming the base document is as follows:
<root>
   <parameter id="1"/>
   <parameter id="2"/>
   <element name="kid1">
      <parameter id="1"/>
   </element>
   <element name="kid2">
       <parameter id="1"/>
       <parameter id="2"/>
       <parameter id="3"/>
   </element>
</root>

I know I can retrieve all root::parameter nodes by allocating a new
xpath context for the document and then evaluating the expression
'/root/parameter'.

Let's say I traverse the document and am now at element with
name="kid2", how do I use the same, already allocated xpath context to
find all element::parameter nodes?
Do I have to specify the element name attribute in the query
('/element[@name='kid2']/parameter')?
Or is there a way for me to use the current 'element' node as the root
for the next xpath query?
(trying to avoid creating a new context for each of the queries on the
same document)


Thanks very much.
Piotrek
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml@...
http://mail.gnome.org/mailman/listinfo/xml

Re: multiple XPath evaluations on the same document

by Martin (gzlist) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 15/10/2009, IRENE SIPIKA <piotreks@...> wrote:
>
> Or is there a way for me to use the current 'element' node as the root
> for the next xpath query?

Set the 'node' member of the xmlXpathContext to the new context node:
<http://xmlsoft.org/html/libxml-xpath.html#xmlXPathContext>

Or with the python bindings, use the setContextNode method on the
xpathContext object.

You can also compile the xpath expression and use xmlXPathCompiledEval
if you're using it more than once.

Martin
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml@...
http://mail.gnome.org/mailman/listinfo/xml