Re: [xml] self-defined namespace nodes



On 05/09/2008, Roopesh Chander <roop forwardbias in> wrote:
I need a bit of help with use of namespaces in the xml tree creation
interface (xmlNewDoc et al.). Specifically, I need to be able to create a an
xml root element in a self-defined namespace, like this:

 <h:html xmlns:h="http://www.w3.org/HTML/1998/html4";>
 something...
 </h:html>

An aside, but the correct namespace uri for html-in-xml-format is
http://www.w3.org/1999/xhtml and getting it wrong is likely to break
whatever you're doing further down the line.

 Per the api, to create the root element with a namespace, I've gotta call
xmlNewNode with an xmlNs* argument. But to create an xmlNs, i need to call
xmlNewNs, which needs the xml element where we want the xmlns tag to be
added (the root element in this case, which we haven't created yet). What is
the indended way of creating such an element using the libxml2 tree api?

See this post and the related thread:
<http://mail.gnome.org/archives/xml/2008-June/msg00069.html>
Also some example code:
<http://mail.gnome.org/archives/xml/2008-August/msg00046.html>

 My present cowardly code includes the namespace in the name, like:

 xmlNode* rootelem = xmlNewNode(NULL, (xmlChar *) "h:html");

No, that is including the prefix in the name, not the namespace, the
upshot of which means you're falling back to pre-Namespaces-in-XML
behaviour. You want to only give local-names (the bit after the colon)
to the node creation functions, along with a namespace object.

Hope the previous mailinglist threads are enough to get you on the right track,

Martin



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