Re: [xml] XPath on a subtree



On Sat, Mar 23, 2013 at 10:50:15AM +0000, Alex Bligh wrote:
I quite often need to find a set of nodes using an XPath expression, then for
each node in that set, do something, then search for zero or more children of
that node.

What I'm interested in is whether the second search can be done with XPath
too. Can I somehow search for children of a particular node? An obvious
way would be to use an XPath context based on that node, but you only
seem to be able to create a context based on a document tree.

Right now, I am manually iterating through children of the node concerned.
That's fine, but only works well when I'm searching for immediate children,
and is obviously not as flexible as XPath.

I can't help feeling I'm missing something here.

  Yup,

  assuming you have a nodeset from the first XPath query,
then create a new XPath context for the document (do not reuse
the old one there) and then for each node in you node set from
the first query do


   ctxt->node = node;
   res = xmlXPathEval("./foo..", ctxt);
   ...

of course the subtree query will have to be relative, i.e. not
starting by /

Daniel


-- 
Daniel Veillard      | Open Source and Standards, Red Hat
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | virtualization library  http://libvirt.org/


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