Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用codesynthesis xsd解析/验证不包含有关xsd信息的xml_Xml_Xsd_Xsd Validation_Codesynthesis - Fatal编程技术网

使用codesynthesis xsd解析/验证不包含有关xsd信息的xml

使用codesynthesis xsd解析/验证不包含有关xsd信息的xml,xml,xsd,xsd-validation,codesynthesis,Xml,Xsd,Xsd Validation,Codesynthesis,我遇到了与中类似的问题。基本上,如果我的xml不包含有关xsd的信息,我就会出错。下面给出了xml、xsd和一个示例程序,给出了错误 hello.xml <?xml version="1.0"?> <hello> <greeting>Hello</greeting> <name>sun</name> <name>moon</name> <name>world</n

我遇到了与中类似的问题。基本上,如果我的xml不包含有关xsd的信息,我就会出错。下面给出了xml、xsd和一个示例程序,给出了错误

hello.xml

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

  <greeting>Hello</greeting>

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

</hello>
<?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>
#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;
  }
}
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
main.cpp

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

  <greeting>Hello</greeting>

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

</hello>
<?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>
#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;
  }
}
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

我想知道是否有一种方法可以避免这个问题,而不需要在xml中指定xsd信息。如果xml不符合xsd,我还希望解析器抛出一个错误(就像现在一样)。

正如Erik Sjoulnd在评论中建议的那样,我添加了以下内容:

xml_schema::properties props;
props.no_namespace_schema_location ("hello.xsd");
unique_ptr<hello_t> h (hello (argv[1],0,props));
xml\u模式::属性道具;
props.no_名称空间_模式_位置(“hello.xsd”);
唯一的(hello(argv[1],0,props));
现在不需要在xml中提及xsd的路径


谢谢你,埃里克

@Erik下的“5.1 XML模式验证和搜索”中有一些文档,我正在阅读,但到目前为止我还没有找到解决方案。可能类似于
props.no_namespace_Schema_location(“file:///home/vishal/testing/hello.xsd");
或使用中的一种技术似乎是
的道具。no_namespace\u schema\u location()
是解决此问题的最快方法。