sample XSD fails with "error: no declaration found for element X"

wallyk picture wallyk · Jun 18, 2011 · Viewed 7.3k times · Source

In spite of being a total newbie in the xml parsing arena, I was able to xsd to create valid c++ and compile and link successfully, but the compiler optimized(?) away the instantiation. So, starting at step one, I try the hello world xml example at CodeSynthesis. But that fails:

[wally@lenovotower xml]$ make hello
xsdcxx cxx-tree hello.xsd
g++ -c -o helloschema.o hello.cxx
g++ -g -o hello -lxerces-c helloschema.o hello.c++
[wally@lenovotower xml]$ ./hello
hello.xml:2:8 error: no declaration found for element 'hello'
hello.xml:4:13 error: no declaration found for element 'greeting'
hello.xml:6:9 error: no declaration found for element 'name'
hello.xml:7:9 error: no declaration found for element 'name'
hello.xml:8:9 error: no declaration found for element 'name'

hello.c++:

#include <iostream>
#include <stdio.h>
#include "hello.hxx"
using namespace std;
int main (void)
{
        try {
                auto_ptr<hello_t> h (hello ("hello.xml"));

                for (hello_t::name_const_iterator i (h->name ().begin()); 
                        i != h->name().end();
                        ++i)
                        cout << h->greeting () << ", " << *i << "!" << endl;    
        }
        catch (const xml_schema::exception& e)
        {
                cerr << e << endl;
                return 1;
        }
        return 0;
}

hello.xml:

<?xml version="1.0"?>
<hello>

  <greeting>Hello</greeting>

  <name>sun</name>
  <name>moon</name>
  <name>world</name>

</hello>

hello.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <xs:complexType name="hello_t">
  <xs:sequence> 
   <xs:element name="greeting" type="xs:string"/>
   <xs:element name="name" type="xs:string" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>

 <xs:element name="hello" type="hello_t"/> 

</xs:schema> 

I think this is exactly what it says to do, but the commands don't work exactly as documented. I discovered xsdcxx seems to do the right thing (unlike xsd which generates C# or vb.net output).

[wally@lenovotower xml]$ xsdcxx --version
CodeSynthesis XSD XML Schema to C++ compiler 3.3.0
Copyright (C) 2005-2010 Code Synthesis Tools CC

Also, I don't include an -I(dir) and it compiles happily. Could it be using the wrong include file somehow?

What am I doing wrong? Maybe xsd isn't the right tool?

Answer

Erik Sj&#246;lund picture Erik Sjölund · Jun 29, 2012

There are some options. The schema location can be provided in the file hello.xml:

<?xml version="1.0"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="hello.xsd">

  <greeting>Hello</greeting>

  <name>sun</name>
  <name>moon</name>
  <name>world</name>

</hello>

Another option is to provide the schema location in the file hello.c++

If we assume the schema file is located at the filepath /some/file/path/hello.xsd

we should instead of writing

auto_ptr<hello_t> h (hello ("hello.xml"));

write

xml_schema::properties properties;
properties.no_namespace_schema_location("file:///some/file/path/hello.xsd");
auto_ptr<hello_t> h (hello ("hello.xml", 0, properties));

You can read more about this in the Codesynthesis FAQ:

Why do I get "error: no declaration found for element 'root-element'" when I try to parse a valid XML document?