样本XSD失败,并显示“错误:找不到元素X的声明”

|| 尽管是xml解析领域的新手,但我仍然可以
xsd
创建有效的
c++
并成功编译和链接,但是编译器优化了实例化。因此,从第一步开始,我在CodeSynthesis尝试使用hello world xml示例。但这失败了:
[wally@lenovotower xml]$ make hello
xsdcxx cxx-tree hello.xsd
g++ -c -o helloschema.o hello.cxx
g++ -g -o hello -lxerces-c helloschema.o hello.c++
[wally@lenovotower xml]$ ./hello
hello.xml:2:8 error: no declaration found for element \'hello\'
hello.xml:4:13 error: no declaration found for element \'greeting\'
hello.xml:6:9 error: no declaration found for element \'name\'
hello.xml:7:9 error: no declaration found for element \'name\'
hello.xml:8:9 error: no declaration found for element \'name\'
hello.c ++:
#include <iostream>
#include <stdio.h>
#include \"hello.hxx\"
using namespace std;
int main (void)
{
        try {
                auto_ptr<hello_t> h (hello (\"hello.xml\"));

                for (hello_t::name_const_iterator i (h->name ().begin()); 
                        i != h->name().end();
                        ++i)
                        cout << h->greeting () << \", \" << *i << \"!\" << endl;    
        }
        catch (const xml_schema::exception& e)
        {
                cerr << e << endl;
                return 1;
        }
        return 0;
}
hello.xml:
<?xml version=\"1.0\"?>
<hello>

  <greeting>Hello</greeting>

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

</hello>
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> 
我认为这正是它要执行的操作,但是这些命令并不能完全按照所记录的那样工作。我发现ѭ6似乎做对了(不同于生成C#或vb.net输出的ѭ0)。
[wally@lenovotower xml]$ xsdcxx --version
CodeSynthesis XSD XML Schema to C++ compiler 3.3.0
Copyright (C) 2005-2010 Code Synthesis Tools CC
另外,我不包含
-I
(dir),并且编译愉快。会以某种方式使用错误的包含文件吗? 我究竟做错了什么?也许“ 0”不是正确的工具?     
已邀请:
        我个人认为Python和lxml的组合非常有价值。您的XML文档和相应的XML模式可以正常工作:
from lxml import etree

xsddoc = etree.parse(\'hello.xsd\')
xsd = etree.XMLSchema(xsddoc)
xml_parser = etree.XMLParser(schema=xsd)
xmldoc = etree.parse(\'hello.xml\', parser=xml_parser)
我没有从中得到任何错误。但是,我要说的是,即使lxml不需要您使用xsi:noNamespaceSchemaLocation,因为它会加载您指定的架构,但只要您不使用命名空间,就应该仍然使用它。仅仅因为一个XML解析器是灵活的,而其他XML解析器却不灵活,这一事实似乎表明您可能已经想通了。如果确实使用名称空间,请使用xsi:schemaLocation而不是xsi:noNamespaceSchemaLocation。还要注意,您必须通过xmlns:xsi属性声明xsi名称空间,以便能够使用xsi:*属性。 使用xsi:noNamespaceSchemaLocation的示例:
<hello xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
       xsi:noNamespaceSchemaLocation=\"hello.xsd\">
  ...
</hello>
    
        有一些选择。可以在文件hello.xml中提供模式位置:
<?xml version=\"1.0\"?>
<hello xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
   xsi:noNamespaceSchemaLocation=\"hello.xsd\">

  <greeting>Hello</greeting>

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

</hello>
另一个选择是在文件hello.c ++中提供架构位置 如果我们假设模式文件位于文件路径/some/file/path/hello.xsd 我们不应该写
auto_ptr<hello_t> h (hello (\"hello.xml\"));
xml_schema::properties properties;
properties.no_namespace_schema_location(\"file:///some/file/path/hello.xsd\");
auto_ptr<hello_t> h (hello (\"hello.xml\", 0, properties));
您可以在Codesynthesis FAQ中阅读有关此内容的更多信息: 当我尝试解析有效​​的XML文档时,为什么会收到“错误:找不到元素'root-element \'\”的声明?     
        编辑:忽略此答案。我将其保留,因为这些注释值得保存。 您的XSD说:
 <xs:complexType name=\"hello_t\">
但是您的XML说:
<hello>
...
</hello>
也许XSD应该是:
 <xs:complexType name=\"hello\">
此外,XML缺少xsi:schemaLocation声明。拥有一个可能会有所帮助。     

要回复问题请先登录注册