1

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.

Vishal Sharma
  • 1,670
  • 20
  • 55
  • There is some documentation in "5.1 XML Schema Validation and Searching" under https://www.codesynthesis.com/projects/xsd/documentation/cxx/tree/guide/ – Erik Sjölund Jul 22 '19 at 12:12
  • @Erik I'm reading it but I've not got the solution till now. – Vishal Sharma Jul 22 '19 at 12:48
  • Maybe something like `props.no_namespace_schema_location("file:///home/vishal/testing/hello.xsd");` or using one of the techniques in https://stackoverflow.com/questions/52275608/xerces-c-validate-xml-with-hardcoded-xsd/52464355#52464355 It seems the `props.no_namespace_schema_location()` is the quickest fix to the problem. – Erik Sjölund Jul 22 '19 at 12:57

1 Answers1

1

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!

Vishal Sharma
  • 1,670
  • 20
  • 55