Re: [libxml++] xpath with namespaces



On Mon, Dec 20, 2004 at 10:10:08PM +0100, Murray Cumming wrote:
> On Mon, 2004-12-20 at 02:51 -0500, Daniel Veillard wrote:
> > > > > >  If I use "//gjob:Jobs" i get "XPath error : Undefined namespace prefix",
> > > > > >  and "//Jobs" just returns an empty set.
> > 
> >   Perfectly normal, the expression uses the gjob prefix and it was not
> > registered.
> 
> Isn't this what
> <gjob:Helping xmlns:gjob="http://www.gnome.org/some-location";>
> does?

 No. The XPath is compiled without a priori knowledge of the instance.
And a given prefix can be defined twice differently in a document

<bad>
  <gjob:Helping xmlns:gjob="http://www.gnome.org/some-location";>
  <gjob:Helping xmlns:gjob="http://www.gnome.org/some-other-location";>
</bad>

 you must define what "gjob" is bound to before "//gjob:Jobs" makes any sense.

> >   http://xmlsoft.org/html/libxml-xpathInternals.html#xmlXPathRegisterNs
> >   at the C level, it may not be available in libxml++
> 
> Thanks Daniel. At the moment, we hide the xmlXPathContext completely
> inside our Node::find(xpath) function, so we would have to create some
> XPathContext object that can be passed around.

  hum, yes the XPath API seems severely broken without ways to associate
prefixes bindings. Maybe a new entry point allowing to define a set of
bindings as well as the XPath expression can be done without refactoring
everything
    find(const char *expression, namespaces);
  where namespaces is a list of couples (prefix/URI)
  
> Is that a concept that exists in a standard, or just something that
> allows libxml users to work around broken documents? Many of the

   it's standard part of XPath evaluation context
     http://www.w3.org/TR/xpath#section-Introduction
it's better to read the spec before designing the API around it :-\

> functions look quite internal. Are there any higher-level documents that
> explain what they might be used for?

  Most of it can be derived from the spec plus simple implementation
decisions. Most of the libxml2 XPath implementation docs are related 
to libxslt 
    http://xmlsoft.org/XSLT/internals.html#XPath

  The prefix binding is really a core property of XPath evaluation,
easy to get wrong (//foo cannot selected a default namespaced node),
and a source of FAQs about XPath

Daniel

-- 
Daniel Veillard      | Red Hat Desktop team http://redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/




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