Re: [xml] libxml .xsd validation problem

Thanks for your quick response and help.

Using a nondefault namespace for the root node seems to have solved the problem, i.e. the generated doc looks 

<?xml version="1.0" encoding="UTF-8"?>
<test_ns:testInstance xmlns:test_ns="http://localhost/test_namespace";>

However setting the namespace of the children to anything other than the result of xmlNewNs(NULL, NULL, 
NULL), I still run into the same problem.  

If I pass NULL as the namespace argument for the children, then it inherits from the root_node and the result 
element element1: Schemas validity error : Element '{http://localhost/test_namespace}element1': This element 
is not expected. Expected is ( element1 ).
generated doc is invalid

Likewise if I try to set it to some non-NULL value I get a similar error saying that the element was not 
expected to have that namespace.

thanks again,

-----Original Message-----
From: Daniel Veillard [mailto:veillard redhat com]
Sent: Wed 9/8/2010 5:22 AM
To: Patrick McClory
Cc: xml gnome org; Joshua Brindle
Subject: Re: [xml] libxml .xsd validation problem
On Tue, Sep 07, 2010 at 04:35:54PM -0400, Patrick McClory wrote:

I'm working on a project which requires validation of xml documents against .xsd schemas.  We both create 
xml documents from scratch, and create xml docs from char * buffers read from a socket.  I've run into 
trouble validating the docs created from buffers, even when the buffers are generated from a document that 
already validated successfully.

        //set the namespace
        root_node->ns = xmlNewNs(root_node, BAD_CAST "http://localhost/test_namespace";, NULL);
        root_node->nsDef = root_node->ns;

  From a tree perspective the root has a namespace, which is defined
as a default namespace

        //create the children
        xmlNsPtr child_ns = xmlNewNs(NULL, NULL, NULL);

  have you checked what this returns ? What is that supposed to mean ?

        xmlNewChild(root_node, child_ns, BAD_CAST "element1", BAD_CAST "foo");
        xmlNewChild(root_node, child_ns, BAD_CAST "element2", BAD_CAST "1");

  while the children have a namespace which is basically not defined.

        xmlSaveFormatFileEnc("generated.xml", doc, "UTF-8", 1);

I dump both docs to output files (generated.xml and buffer.xml), and I confirmed that they're the same on 
disk using diff.

  when you serialize, I bet the child_ns does not appear.

The problem seems to be that when libxml reads from the buffer it attaches the parent namespace to the 
children (if it isn't specified), which later causes validation to fail.  Is this a common problem?  Is 
there a standard workaround for this?

The problem is that basically you defined the root namespace as a
default namespace (no prefix), that the broken namespace used for the
children is not serialized and as a result the output document have
the children inheriting the default namespace from root.

Don't use a default namespace on the root. and fix the children
namespace definition.


Daniel Veillard      | libxml Gnome XML XSLT toolkit
daniel veillard com  | Rpmfind RPM search engine | virtualization library

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