样本XSD失败,并且“错误:对于元素X找不到声明” [英] sample XSD fails with "error: no declaration found for element X"

查看:834
本文介绍了样本XSD失败,并且“错误:对于元素X找不到声明”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在xml解析领域是一个新手,我能够 xsd 创建有效的 c ++ 并且编译和链接成功,但是编译器优化(?)离开实例化。因此,从第一步开始,我尝试 hello世界 xml示例在代码合成。但是失败:

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 ++:

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> 

我认为这正是它要做的,但是命令不能完全按照文档。我发现 xsdcxx 似乎做正确的事情(不同于生成C#或vb.net输出的 xsd )。 p>

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

此外, code> -I (dir),它编译愉快。是否会以某种方式使用错误的包含文件?

Also, I don't include an -I(dir) and it compiles happily. Could it be using the wrong include file somehow?

我做错了什么?

推荐答案

个人我发现Python和lxml的组合是非常无价的。您的XML文档和相应的XML模式工作正常:

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

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:*属性。

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.

使用xsi:noNamespaceSchemaLocation: p>

An example using xsi:noNamespaceSchemaLocation:

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

这篇关于样本XSD失败,并且“错误:对于元素X找不到声明”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆