我得到这个错误
cvc-complex-type.2.4。a:发现以元素"Value"开头的无效内容。'{"http://www.example.org/fooBar":Value}'中的一个是预期。
我使用的xml实例是:
<?xml version="1.0" encoding="UTF-8"?>
<Foo xmlns="http://www.example.org/Foo">
<Element>some Data</Element>
<Element>some Data</Element>
<Element>some Data</Element>
<Bar xmlns="http://www.example.org/Bar">
<Item>
<Value>My Value</Value>
</Item>
</Bar>
<Element>some Data</Element>
<Element>some Data</Element>
<Element>some Data</Element>
</Foo>
这是Bar
模式:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:fooBar='http://www.example.org/fooBar' targetNamespace="http://www.example.org/Bar"
xmlns:Bar="http://www.example.org/Bar" elementFormDefault='qualified'>
<import namespace='http://www.example.org/fooBar' />
<element name="Bar">
<complexType>
<sequence>
<element name="Item" type="fooBar:itemType" />
</sequence>
</complexType>
</element>
</schema>
这是导入的fooBar
模式:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/fooBar" xmlns:fooBar="http://www.example.org/fooBar"
elementFormDefault='qualified'>
<complexType name="itemType">
<sequence>
<element name="Value" type="string" />
</sequence>
</complexType>
</schema>
我想它没有得到名称空间的权利?但是为什么呢?如果我将itemType
从fooBar
模式拖到Bar
模式(Value
停留在fooBar中),错误是相同的,但这次它抱怨Value
元素。这是一个bug还是我做错了什么?它似乎不能处理来自导入方案的类型。
为了说明我想做什么,这个例子可能会有所帮助:
<?xml version="1.0" encoding="UTF-8"?>
<Foo xmlns="http://www.example.org/Foo">
<Element>some Data</Element>
<Element>some Data</Element>
<Element>some Data</Element>
<Bar xmlns="http://www.example.org/Bar" xmlns:mon="http://www.example.org/fooBar">
<Item>
<mon:Value>My Value</mon:Value>
</Item>
</Bar>
<Element>some Data</Element>
<Element>some Data</Element>
<Element>some Data</Element>
</Foo>
我想让用户(编写xml)不必知道Value
所在的名称空间。他应该直接进入Bar
,然后像"什么都没发生"一样走到下面。这似乎是不可能的。
Bar元素位于命名空间"http://www.example.org/Bar"中,但模式要求它位于命名空间"http://www.example.org/fooBar"
对于您所展示的模式文档和XML实例,错误在于Value元素应该位于名称空间"http://www.example.org/fooBar"中,而实际上它位于名称空间"http://www.example.org/Bar"中。这与错误消息无关;根据你的描述,我怀疑错误信息来自不同的尝试。
Value应该在"http://www.example.org/fooBar"中的原因是,这是在其中声明Value的模式文档的targetNamespace,并且模式文档使用elementFormDefault="qualified",这表明targetNamespace适用于全局和局部元素声明。
看来我想做的事是不可能的。你必须像我在上一个例子中展示的那样做。感谢freenode上的#xml频道!