示例 XSD 失败并显示"error: no declaration found for element X"
sample XSD fails with "error: no declaration found for element X"
尽管我是xml解析领域的新手,但我能够xsd
创建有效的c++
并成功编译和链接,但编译器优化了实例化。因此,从第一步开始,我在CodeSynthesis中尝试helloworldxml示例。但失败了:
[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'
你好.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>
你好.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
不是合适的工具?
有一些选项。模式位置可以在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常见问题解答:中阅读更多关于这方面的信息
当我尝试解析有效的XML文档时,为什么会出现"错误:找不到元素"根元素"的声明"
就我个人而言,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解析器是灵活的,其他解析器可能不是,事实上,您可能已经找到了困难的方法。如果确实使用命名空间,请使用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>
编辑:忽略此答案。我不提了,因为这些评论值得保存
您的XSD显示:
<xs:complexType name="hello_t">
但是您的XML显示:
<hello>
...
</hello>
也许XSD应该是:
<xs:complexType name="hello">
此外,XML缺少xsi:schemaLocation声明。有一个可能会有所帮助。
- OpenCV Android C++ imwrite not found
- 解决"ld: library not found for -ltensorflow_framework.2.3.0"
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 处理"no operator found"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- Declaration of IntRect SFML
- C++初学者:为什么我的编译器根据我的循环返回"name not found"?
- _BitScanForward64 can not be found
- 如何区分C++中的"using declaration"和"using directive"?
- 为什么别名声明不是有效的 init 语句(/simple-declaration)?
- 使用提升 asio 时出现"resolve: Host not found (authoritative)"异常
- 静态数据成员:它"const declaration / constexpr definition"起作用?
- 为什么 VScode 在 .h 文件中显示"'iostream' file not found"?
- SFINAE:"enable_if cannot be used to disable this declaration"
- 如何理解"abstract-declarator containing an ellipsis shall only be used in a parameter-declaration"
- 使用自定义 nodejs 插件时的"The specified module could not be found"
- 如何修复"expected a declaration"?
- Boost.Python 和导入 dll,"The specified module could not be found"
- 为什么我在" 'X' identifier not found' "上收到错误
- 示例 XSD 失败并显示"error: no declaration found for element X"