示例 XSD 失败并显示“错误:未找到元素 X 的声明”
尽管我是 xml 解析领域的新手,但我能够 xsd
创建有效的 c++
并成功编译和链接,但编译器优化了(?)实例化。因此,从第一步开始,我尝试 hello CodeSynthesis 中的 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>
我认为这正是它所说的,但这些命令并不完全按照文档工作。我发现 xsdcxx 似乎做了正确的事情(与生成 C# 或 vb.net 输出的 xsd 不同)。
[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) 并且它可以顺利编译。是否可能以某种方式使用了错误的包含文件?
我做错了什么?也许 xsd 不是正确的工具?
In spite of being a total newbie in the xml parsing arena, I was able to xsd
to create valid c++
and compile and link successfully, but the compiler optimized(?) away the instantiation. So, starting at step one, I try the hello world xml example at CodeSynthesis. But that fails:
[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>
I think this is exactly what it says to do, but the commands don't work exactly as documented. I discovered xsdcxx
seems to do the right thing (unlike xsd
which generates C# or vb.net output).
[wally@lenovotower xml]$ xsdcxx --version
CodeSynthesis XSD XML Schema to C++ compiler 3.3.0
Copyright (C) 2005-2010 Code Synthesis Tools CC
Also, I don't include an -I
(dir) and it compiles happily. Could it be using the wrong include file somehow?
What am I doing wrong? Maybe xsd
isn't the right tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一些选择。可以在文件 hello.xml 中提供架构位置:
另一种选择是在文件 hello.c++ 中提供架构位置,
如果我们假设架构文件位于文件路径 /some/file/path/hello.xsd
我们应该不写
write
您可以在 Codesynthesis 常见问题解答中阅读有关此内容的更多信息:
为什么我会收到“错误:无声明”当我尝试解析元素时发现元素“根元素”有效的 XML 文档?
There are some options. The schema location can be provided in the file hello.xml:
Another option is to provide the schema location in the file hello.c++
If we assume the schema file is located at the filepath /some/file/path/hello.xsd
we should instead of writing
write
You can read more about this in the Codesynthesis FAQ:
Why do I get "error: no declaration found for element 'root-element'" when I try to parse a valid XML document?
我个人认为 Python 和 lxml 的结合非常有价值。您的 XML 文档和相应的 XML 模式工作得很好:
我没有从中得到任何错误。不过,我要说的是,即使 lxml 不要求您使用 xsi:noNamespaceSchemaLocation 因为它会加载您指定的架构,但只要您不使用命名空间,您仍然应该使用它。仅仅因为一个 XML 解析器很灵活,其他解析器就可能不灵活,这一事实似乎您可能已经找到了困难的方法。如果您确实使用命名空间,请使用 xsi:schemaLocation 而不是 xsi:noNamespaceSchemaLocation。另请注意,您必须通过 xmlns:xsi 属性声明 xsi 命名空间才能使用 xsi:* 属性。
使用 xsi:noNamespaceSchemaLocation 的示例:
Personally I find the combination of Python and lxml to be pretty invaluable. Your XML document and the corresponding XML schema work just fine:
I didn't get any errors from that. I will say, however, that even though lxml doesn't require you to use xsi:noNamespaceSchemaLocation since it loads the schema you specify, you should still use it as long as you're not using namespaces. Just because one XML parser is flexible, others might not be, a fact that it seems you might have figured out the hard way. If you do use namespaces, use xsi:schemaLocation instead of xsi:noNamespaceSchemaLocation. Also note that you MUST declare the xsi namespace via the xmlns:xsi attribute to be able to use the xsi:* attributes.
An example using xsi:noNamespaceSchemaLocation:
编辑:忽略这个答案。我将其保留,因为这些评论值得保存。
<罢工>
你的 XSD 说:
但你的 XML 说:
也许 XSD 应该是:
另外,XML 缺少 xsi:schemaLocation 声明。拥有一个可能会有所帮助。
EDIT: Ignore this answer. I'm leaving it up because the comments are worth saving.
Your XSD says:
but your XML says:
Perhaps the XSD should be:
Also, the XML is missing an xsi:schemaLocation declaration. It might help to have one.