【问题标题】:Parsing/validating xml not including information about xsd using codesynthesis-xsd使用codesynthesis-xsd解析/验证xml不包括有关xsd的信息
【发布时间】:2019-11-30 04:26:36
【问题描述】:

我遇到了与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,我还希望解析器向我抛出错误(就像现在一样)。

【问题讨论】:

标签: xml xsd xsd-validation codesynthesis


【解决方案1】:

按照 Erik Sjolund 在 cmets 中的建议,我添加了以下内容:

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

现在就不用在xml中提及xsd的路径了。

谢谢埃里克!

【讨论】:

    猜你喜欢
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多