[xml] XPath and default namespaces (bet you're sick of this by now :) )

Dear Daniel (and other list readers),
        I have been playing with the libxml implementation, attempting
to write something I can use to bind xml documents into structures
        Firstly: C'est fantastique!

But secondly, I have fallen afoul of the default namespace problem.
I saw many postings, where Daniel and others told everyone that it is all OK and not buggy especially after v 2.4, but I didn't quite
grasp the workaround.

I looked at the libxml code (v 2.5.1)
and find that in the file xpath.c:

---- snip snip ----
xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
                           const xmlChar *ns_uri) {
    if (ctxt == NULL)
    if (prefix == NULL)

---- snip snip ----

This is confusing because, in the tree, the default namespace
as far as I seem to remember has a NULL prefix. As quoted on

An XML namespace. Note that prefix == NULL is valid, it defines the default namespace within the subtree (until overridden).

Hence my natural inclination for defining the default namespace for XPath would be to pass a NULL prefix to xmlXPathRegisterNs, which of course the above code snippet immediately rejects as erroneous.

As others have covered: registering "" doesn't seem to work.

What I can do, is register some dummy namespace:

    xmlNodePtr current_node=(get this node some magic way);
    xmlNsPtr nsdefptr = current_node->nsDef;
    if(nsdefptr != NULL) {
         if(nsdefptr->prefix == NULL) {
                               (const xmlChar *)"dummy",

    but in this case I have to prefix all my XPath query
element id's (which are meant to be in the default namespace)
with dummy: (eg  <foo/> needs XPath: /dummy:foo) This is not nice.

I know you all are sick of this query, but would you please
help me out?

        Many thanks,

Dr Balint Joo                         Post Doctoral Research Fellow
School of Physics
University of Edinburgh
Mayfield Road, Edinburgh EH9 3JZ
Scotland UK
Tel: 0131 650 6469 (from UK) +44-131-650-6469 (from outwith UK)
Fax: 0131 650 5902 (from UK) +44-131-650-5902 (from outwith UK)
email: bj ph ed ac uk           bj phys columbia edu
WWW  : http://www.ph.ed.ac.uk/~bj

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