样本XSD失败的“错误:没有发现元素X的声明”

时间:2022-05-15 17:17:07

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:

尽管在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:

hello.xml:

<?xml version="1.0"?>
<hello>

  <greeting>Hello</greeting>

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

</hello>

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> 

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).

我认为这正是它所要做的,但是这些命令并不像文档那样精确。我发现xsdcxx似乎做了正确的事情(不像xsd生成c#或vb.net输出)。

[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?

另外,我不包括I(dir),它编译得很好。可能是使用错误的包含文件吗?

What am I doing wrong? Maybe xsd isn't the right tool?

我做错了什么?也许xsd不是正确的工具?

3 个解决方案

#1


1  

Personally I find the combination of Python and lxml to be pretty invaluable. Your XML document and the corresponding XML schema work just fine:

我个人认为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)

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.

我没有从中得到任何错误。但是,我要说的是,即使lxml不要求您使用xsi:非amespaceschemalocation,因为它加载了您指定的模式,您仍然应该使用它,只要您没有使用名称空间。仅仅因为一个XML解析器是灵活的,其他的可能不是,一个事实是,您可能已经找到了困难的方法。如果确实使用名称空间,请使用xsi:schemaLocation而不是xsi:非amespaceschemalocation。还要注意,您必须通过xmlns:xsi属性声明xsi名称空间,以便能够使用xsi:*属性。

An example using xsi:noNamespaceSchemaLocation:

一个例子使用xsi:noNamespaceSchemaLocation:

<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="hello.xsd">
  ...
</hello>

#2


3  

There are some options. The schema location can be provided in the file hello.xml:

有一些选项。可以在文件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>

Another option is to provide the schema location in the file hello.c++

另一种方法是在文件hello.c++中提供模式位置。

If we assume the schema file is located at the filepath /some/file/path/hello.xsd

如果我们假设模式文件位于filepath /some/file/path/hello.xsd中。

we should instead of writing

我们不应该写作。

auto_ptr<hello_t> h (hello ("hello.xml"));

write

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));

You can read more about this in the Codesynthesis FAQ:

你可以在Codesynthesis FAQ里读到更多关于这方面的内容:

Why do I get "error: no declaration found for element 'root-element'" when I try to parse a valid XML document?

为什么会出现“错误:在尝试解析一个有效的XML文档时,没有发现元素‘根元素’的声明”?

#3


1  

EDIT: Ignore this answer. I'm leaving it up because the comments are worth saving. Your XSD says:

编辑:忽略这个答案。我把它留了下来,因为这些评论值得保存。您的XSD表示:

 <xs:complexType name="hello_t">

but your XML says:

但是你的XML表示:

<hello>
...
</hello>

Perhaps the XSD should be:

也许XSD应该是:

 <xs:complexType name="hello">

Also, the XML is missing an xsi:schemaLocation declaration. It might help to have one.

另外,XML缺少一个xsi:schemaLocation声明。有一个可能会有帮助。

#1


1  

Personally I find the combination of Python and lxml to be pretty invaluable. Your XML document and the corresponding XML schema work just fine:

我个人认为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)

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.

我没有从中得到任何错误。但是,我要说的是,即使lxml不要求您使用xsi:非amespaceschemalocation,因为它加载了您指定的模式,您仍然应该使用它,只要您没有使用名称空间。仅仅因为一个XML解析器是灵活的,其他的可能不是,一个事实是,您可能已经找到了困难的方法。如果确实使用名称空间,请使用xsi:schemaLocation而不是xsi:非amespaceschemalocation。还要注意,您必须通过xmlns:xsi属性声明xsi名称空间,以便能够使用xsi:*属性。

An example using xsi:noNamespaceSchemaLocation:

一个例子使用xsi:noNamespaceSchemaLocation:

<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="hello.xsd">
  ...
</hello>

#2


3  

There are some options. The schema location can be provided in the file hello.xml:

有一些选项。可以在文件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>

Another option is to provide the schema location in the file hello.c++

另一种方法是在文件hello.c++中提供模式位置。

If we assume the schema file is located at the filepath /some/file/path/hello.xsd

如果我们假设模式文件位于filepath /some/file/path/hello.xsd中。

we should instead of writing

我们不应该写作。

auto_ptr<hello_t> h (hello ("hello.xml"));

write

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));

You can read more about this in the Codesynthesis FAQ:

你可以在Codesynthesis FAQ里读到更多关于这方面的内容:

Why do I get "error: no declaration found for element 'root-element'" when I try to parse a valid XML document?

为什么会出现“错误:在尝试解析一个有效的XML文档时,没有发现元素‘根元素’的声明”?

#3


1  

EDIT: Ignore this answer. I'm leaving it up because the comments are worth saving. Your XSD says:

编辑:忽略这个答案。我把它留了下来,因为这些评论值得保存。您的XSD表示:

 <xs:complexType name="hello_t">

but your XML says:

但是你的XML表示:

<hello>
...
</hello>

Perhaps the XSD should be:

也许XSD应该是:

 <xs:complexType name="hello">

Also, the XML is missing an xsi:schemaLocation declaration. It might help to have one.

另外,XML缺少一个xsi:schemaLocation声明。有一个可能会有帮助。