我遇到了一个类似于this question的问题。基本上,如果我的xml不包含有关xsd的信息,我就会得到错误。下面给出了xml、xsd和一个给出错误的示例程序。
hello.xml
<?xml version="1.0"?>
<hello>
<greeting>Hello</greeting>
<name>sun</name>
<name>moon</name>
<name>world</name>
</hello>如果我在开始时用下面的代码替换了“hello”标签,那么程序就会运行得很好。
<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我想知道是否有一种方法可以避免这个问题,而不需要在xml中指定xsd信息。如果xml不符合xsd,我还希望解析器抛出一个错误(就像现在一样)。
发布于 2019-08-01 10:09:18
正如Erik Sjolund在评论中所建议的那样,我补充了以下内容:
xml_schema::properties props;
props.no_namespace_schema_location ("hello.xsd");
unique_ptr<hello_t> h (hello (argv[1],0,props));现在没有必要在xml中提到xsd的路径。
谢谢埃里克!
https://stackoverflow.com/questions/57141880
复制相似问题