Re: [xml] how to set or reset a namespace prefix?



Krepis P. wrote:
Would the following code snipet be correct (logic-wise)?

xmlNsPtr namespace=xmlDocGetRootElement(doc)->ns;
if(!xmlStrEqual(namespace->prefix,"xhtml"))
{
        if(!xmlDictExists(doc->dict,"xhtml"))
        {
                if(namespace->prefix)xmlFree(namespace->prefix);
                xmlChar *newPrefix=xmlMalloc(5);
                memcpy(newPrefix,"xhtml",5);
                namespace->prefix=newPrefix;

also this is imho better (forgot that tailing '\0';
                namespace->prefix=xmlMalloc(6);
                memcpy(namespace->prefix,"xhtml\0",6);

        }
}

Also, would the change apply to each child node automatically, or
would I have to walk through the tree and assign the new prefix in the
same manner?

I belive you need to to do this foreach node.

Stefan


On 1/13/06, Daniel Veillard <veillard redhat com> wrote:

On Thu, Jan 12, 2006 at 11:31:43PM -0500, Krepis P. wrote:

I need to either set or change a prefix for a namespace on an existing
document tree. There's plenty of ways to get the information on the
namespace, but I'm not sure how best to change the namespace prefix.
Do I just assign a new xmlChar * to xmlNsPtr->prefix, or do I need to
use a function for that (or do anything else along with it)?

 yes replace the prefix in the xmlNs. Check if it doesn't come from the
document dictionnary, if no xmlFree() it, and then replace it by a new
xmlMalloc'ed string or by a string from the dictionnary.
 The operation has no public entry point, usually namespaces are immutable
as the prefix should not be important and changing the namespace name affects
all referencing nodes in the document.

Daniel

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


_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml




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