Re: [xml] Namespace Handling



Hello,

  I find hard to accept it as right. You can define whatever semantic
you want for the namespace binding since there is none coming from the
spec but prefix "a" may be associated to 10 different namespaces in the
document, picking the fist one in document order doesn't sound right at
all...

I suspected this. No doubt funnies would have occurred eventually with
sufficiently complex xml documents.

This seems to me a great waste of time.
 Yes this sounds like a terrible waste.

Glad we agree :)

  Simply walk the current node and its ancestors, the cost will be linear
to the depth of the tree, not it size !
  There is already a call doing this exported from the tree module
/**
 * xmlGetNsList:
 * @doc:  the document
 * @node:  the current node
 *
 * Search all the namespace applying to a given element.
 * Returns an NULL terminated array of all the #xmlNsPtr found
 *         that need to be freed by the caller or NULL if no
 *         namespace if defined
 */
xmlNsPtr *
xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node);

Daniel




I saw this function and wondered what it did. Let me see if I have this
straight:

When I parse a document my 'current' node is the 'root' node. I then

void newSnarfNamespaces(xmlDocPtr doc, xmlXPathContextPtr context_ptr)
{
        xmlNsPtr *ns_ptr_array;
        xmlNsPtr *shadow;
        xmlNodePtr root;
        int i;

        root=xmlDocGetRootElement(doc);

        // Get top level namespaces
        ns_ptr_array = xmlGetNsList(doc,root);
        
        // Walk list and register xpath
        shadow = ns_ptr_array;
        while( shadow != NULL ) {
            xmlXPathRegisterNs(context_ptr, (*shadow)->prefix, (*shadow)->href);
            shadow++; // Next
        }
        
        // free the list returned by xmlGetNsList()
        if( ns_ptr_array != NULL ) {
          xmlFreeNsList(*ns_ptr_array);
        }
        
}
        
Then I can do queries from there with namespaces
(eg //bj:gnus )?

I am not sure how any other node apart from the root node can become current.
I suppose when I do an XPath query from the root, and a single element is
returned in the node list, I can make that the 'current' node and I can search
back.

Alternatively, I can try to get to the last element of the whole tree with
repeated calls of getLastChild, and when I reach the bottom do the getNsList?
(This sounds more like the searching of the depth of the tree you mentioned)

parent=xmlDocGetRootElemet(doc);
do {
        // get last child
        last = xmlGetLastChild(parent);


        // Can go further
        if( last != NULL ) {
          // next level
          parent = last;
        }

} while( last != NULL );

// Correct for overshoot
last = parent;


This sounds wrong too somehow.

Sorry to be such a dummy. This is really the last hurdle for me and
would like to get it done :)

All the best,
        Baliint

--
-------------------------------------------------------------------
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]