[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: [xml] Set Default Namespace
- From: "Martin (gzlist)" <gzlist googlemail com>
- To: "Niels Van Hoef" <niels vanhoef gmail com>
- Cc: xml gnome org
- Subject: Re: [xml] Set Default Namespace
- Date: Mon, 25 Aug 2008 19:58:19 +0100
On 25/08/2008, Niels Van Hoef <niels vanhoef gmail com> wrote:
>
> I tried you suggestion even before posting this question but a small
> test program yields the following results:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <site xmlns:="default">
> <node>element</node>
> </site>
This looks like you've passed a pointer to an empty string, rather
than a null pointer.
> Where as I would suspect:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <site xmlns="default">
> <node>element</node>
> </site>
>
> I'm rather new to XML but I believe the proper declaration of the
> default name space should be without the double-colon, right?
Right, the first is not correct by the namespaces spec. Perhaps
libxml2 could be a little more polite by refusing to construct
namespaces that will lead to bad output.
I've written a short example (see simple.c attached) and the output it
should produce (see result.txt attached),
Martin
#include "libxml/parser.h"
#include "libxml/xmlsave.h"
xmlDocPtr simple_create(xmlChar* localname, xmlChar* prefix, xmlChar* ns)
{
xmlDocPtr doc = xmlNewDoc(0);
if (doc && localname)
{
xmlNodePtr rootnode = xmlNewChild((xmlNodePtr)doc, 0, localname, 0);
if (rootnode && ns)
{
xmlNsPtr nsnode = xmlNewNs(rootnode, ns, prefix);
if (nsnode)
{
xmlSetNs(rootnode, nsnode);
}
}
}
return doc;
}
void simple_test(char* localname, char* prefix, char* ns)
{
xmlDocPtr doc = simple_create((xmlChar*)localname, (xmlChar*)prefix, (xmlChar*)ns);
if (doc)
{
xmlSaveCtxtPtr savectxt = xmlSaveToFd(1, 0, XML_SAVE_NO_DECL);
if (savectxt)
{
xmlSaveDoc(savectxt, doc);
xmlSaveClose(savectxt);
}
xmlFreeDoc(doc);
}
}
int main()
{
/* With prefix, fine */
simple_test("root", "xmp", "http://example.com/ns");
/* No prefix, fine */
simple_test("root", 0, "http://example.com/ns");
/* Empty prefix, bad! */
simple_test("root", "", "http://example.com/ns");
return 0;
}
<xmp:root xmlns:xmp="http://example.com/ns"/>
<root xmlns="http://example.com/ns"/>
<:root xmlns:="http://example.com/ns"/>
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]