xmlxsdxsd-validationcodesynthesis

Parsing/validating xml not including information about xsd using codesynthesis-xsd


I've encountered a similar problem as in this question. Basically if my xml does not contain the information about the xsd, I get errors. Given below are the xml,xsd and a sample program giving me the errors.

hello.xml

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

  <greeting>Hello</greeting>

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

</hello>

Had I replaced the 'hello' tag in the beginning with the following then the program would've run just fine.

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

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>

main.cpp

#include <iostream>
#include "hello.hxx"

using namespace std;

int
main (int argc, char* argv[])
{
  try
  {
    unique_ptr<hello_t> h (hello (argv[1]));

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

Error

exception caught(/home/vishal/testing/hello.xml:2:8 error: no declaration found for element 'hello'
/home/vishal/testing/hello.xml:4:13 error: no declaration found for element 'greeting'
/home/vishal/testing/hello.xml:6:9 error: no declaration found for element 'name'
/home/vishal/testing/hello.xml:7:9 error: no declaration found for element 'name'
/home/vishal/testing/hello.xml:8:9 error: no declaration found for element 'name'): instance document parsing failed

I wanted to know if there's a way to circumvent this problem without the need of specifying the xsd information in the xml. I also want that the parser throws me an error(just like it does now) if the xml does not conform to the xsd.


Solution

  • As suggested by Erik Sjolund in the comments, I added the following:

    xml_schema::properties props;
    props.no_namespace_schema_location ("hello.xsd");
    unique_ptr<hello_t> h (hello (argv[1],0,props));
    

    and now there's no need to mention the path of the xsd in the xml.

    Thanks Erik!