Re: [xml] root node with namespace

On 21/06/2008, dhk <dhkuhl optonline net> wrote:
How is a root node created with a namespace?  It seem that to make the root
node I'd need to have a namespace to use with xmlNewNode() and to make a
namespace I would need to call xmlNewNs() which requires a node as indicated
in the following two functions.

 xmlNodePtr xmlNewNode(xmlNsPtr ns, const xmlChar *name)

 xmlNsPtr xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar

 Which call comes first and how do you create your first node with a


In general, you can create the namespace on the root element and use
that for all descendants.

I don't know the right answer for creating a prefixed-namespaced root
element, but when I ran across this chicken-and-egg thing a while back
and didn't want to get distracted looking up the answer, I did (using
the python bindings as it's easy to see what's going on, 's pretty
much the same with C):

import libxml2
doc = libxml2.newDoc(None)
doc.newChild(None, "rootnode", None)
<xmlNode (rootnode) object at 0xcb17d8>
rootnode = _
rootnode.newNs("";, "example")
<xmlNs (example) object at 0xcd08f0>
ns = _
print doc
<?xml version="1.0"?>
<example:rootnode xmlns:example=""/>

Docs for these functions:


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