Re: [xml] Setting a context from a patricular node




On Dec 21, 2007 1:49 PM, James Dennett <jdennett technocom-wireless com> wrote:
(Top-posting fixed.)

Sorry,  I did this through Nabble...


eric_w wrote:

> James Dennett wrote:
> >
> >> -----Original Message-----
> >> From: xml-bounces gnome org [mailto: xml-bounces gnome org] On
Behalf
> > Of
> >> Abhishek Jain
> >> Sent: Wednesday, November 07, 2007 3:22 PM
> >> To: xml gnome org
> >> Subject: [xml] Setting a context from a patricular node
> >>
> >> Hi,
> >>
> >> I want to set the xmlXPathContext from a particular node say
> > xmlNodePtr
> >> node1 in a xmlDocPtr doc so that my XPath is evaluated in the
subtree
> > of
> >> node1.
> >>
> >> Please let me know how can i do that.
> >
> > While I'd be more comfortable if I could find this documented, so
far as
> > I can tell the recommended approach is to set the "node" field of
the
> > xmlXPathContext, i.e., context->node = node1; in your case.
> >
> > I'd also note that some library calls will change the node, so you
> > should reset it before reusing a context.
> >
> > -- James
>
> Doesn't this sequence
>
>      doc = xmlTextReaderCurrentDoc( reader);
>      pathContext = xmlXPathNewContext( doc);
>
> accomplish the same thing?

I don't see how it could, given that it does not even mention
a "current node".  But I could be missing something.  Could
you show how you'd implement code to evaluate an XPath relative
to a specified node within a document?

> Or are you suggesting that is is sufficient to
> just set the xmlNodePtr
> in the path context structure?

I don't completely understand your question.  Obviously it is
Necessary to initialize the context using xmlXPathNewContext;
after that, I believe the context node can be changed within
the context, and I don't know of a function call to do so,
although some function calls change the current node as a
side-effect.

Maybe this is a misunderstanding on my part of what exactly the libxml2 APIs are doing. (I can not be the first to notice that the functional descriptions are sparse at best.)

I assumed that the context was propagated from the xmlReaderPtr at the time

     doc = xmlTextReaderCurrentDoc( reader);

is called. I was doing this after several iterations of

   while ( ret = 1) {
     ret = xmlTextReaderRead( reader);
     if ( XML_READER_NODE_TYPE_ELEMENT = xmlTextReaderNodeType( reader)) {
            ...
     }
   }

If I need to explicitly set the path node context, that might explain some bugs I am encountering... Note that none of the examples on the libxml2 site seem to indicate that explicitly setting the node in the xmlPathContext is necessary. Did I miss something???

  --Eric


-- James




--
E r i c   W e s t
cruft gmail com
Boston, Ma USA

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