"Re: [xml] Removing namespace attributes"


on 5/18/2004 7:55 PM Siyan, Karanjit (NIH/NLM/NCBI) wrote:
I am using libxml2 and have a situation where I need to strip the name space
attributes from a node:


<front xmlns:aml="http://exampl1.org"; xmlns:wxa="http://www.example2.org"; >




Above needs to become just:




I tried using xmlUnsetProp() but this is not for the name space
attributes... and does not work.

Yes, namespace information is held in node->nsDef, a chained list of 
xmlNs structs.

Any suggestions?

Suggestion (sorry, in Pascal):

function idom_unlinkNamespaceByPrefix(const aNodePtr: xmlNodePtr; const 
aPrefix: pchar): xmlNsPtr;
// This function unlinks the specified namespace structure from the 
nsdef list of an element.
// The removed ns is returned and not freed.
   prevNsPtr, nsPtr: xmlNsPtr;
   result := nil;
   if (aNodePtr.type_ <> XML_ELEMENT_NODE) then
   nsPtr := aNodePtr.nsDef;
   prevNsPtr := nil;
   while (nsPtr <> nil) do begin
     if (xmlStrEqual(nsPtr.prefix, aPrefix)) then begin
       if (nsPtr = aNodePtr.nsDef) then
         aNodePtr.nsDef := nsPtr.next
         prevNsPtr.next := nsPtr.next;
       result := nsPtr;
     prevNsPtr := nsPtr;
     nsPtr := nsPtr.next;

But take care: Elements and attributes in the subtree might reference 
those ns entries, since in libxml2 the namespace of an element/attribute 
is directly bound to a namespace declared in the ancestor-or-self axis. 
So if you want to move up your ns declarations to an ancestor element 
(assuming you manually add them to the dsDef list of that element), you 
need to call xmlReconciliateNs afterwards to bind the namespaces to your 
new ns declarations. Note that AFAIK libxml2 is not intended to shuffle 
around namespace declarations; it tries to manage them automatically, 
which in turn makes explicit positioning of ns declarations a bit tricky.

Hope that helps...



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