来自导入模式的外部complexType在xml实例中引发错误

时间:2014-08-23 15:01:29

标签: xml xsd

我收到此错误

  

cvc-complex-type.2.4.a:找到以元素'值'开头的无效内容。其中一个{" http://www.example.org/fooBar":价值}'是    预期

我使用的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>

我认为它没有让命名空间正确吗?但为什么? 如果我将itemTypefooBar架构拖到Bar架构(Value停留在fooBar中)错误是相同的,但这次它抱怨{{1元素。 这是一个错误还是我做错了什么?它似乎无法处理来自导入方案的类型。

编辑:

为了澄清我想做什么,这个例子可能会有所帮助:

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" 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> 所在的名称空间。他应该只需要Value,然后就可以到达那里,就像#34;什么都没发生&#34;。 这似乎是不可能的。

3 个答案:

答案 0 :(得分:0)

您的Bar元素位于名称空间“http://www.example.org/Bar”中,但您的架构要求它位于名称空间“http://www.example.org/fooBar

答案 1 :(得分:0)

使用模式文档和显示它们的XML实例,错误是Value元素应该在名称空间&#34; http://www.example.org/fooBar&#34;当它实际上在名称空间&#34; http://www.example.org/Bar&#34;。这与错误消息没有关联;从你的解密我怀疑错误信息来自不同的尝试。

值应该在&#34; http://www.example.org/fooBar&#34;这是声明Value的模式文档的targetNamespace,模式文档使用elementFormDefault =&#34; qualified&#34;表示targetNamespace适用于全局和本地元素声明。

答案 2 :(得分:0)

似乎我想做的事情是不可能的。你必须这样做,就像我在上一个例子中所展示的那样。 感谢freenode上的#xml频道!