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
*prefix)

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

 thanks,
 Dave

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("http://www.example.com/ns";, "example")
<xmlNs (example) object at 0xcd08f0>
ns = _
rootnode.setNs(ns)
print doc
<?xml version="1.0"?>
<example:rootnode xmlns:example="http://www.example.com/ns"/>

Docs for these functions:
<http://xmlsoft.org/html/libxml-tree.html#xmlNewDoc>
<http://xmlsoft.org/html/libxml-tree.html#xmlNewChild>
<http://xmlsoft.org/html/libxml-tree.html#xmlNewNs>
<http://xmlsoft.org/html/libxml-tree.html#xmlSetNs>

Martin



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