命名空间组件不可引用

时间:2015-03-11 12:34:17

标签: xml xsd namespaces

下面是我的部分XML Schema

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Procurement" 
xmlns:tns="http://www.example.org/Procurement" elementFormDefault="qualified">

<element name="CountryCode">
    <simpleType>
        <restriction base="string">
            <enumeration value="US" />
            <enumeration value="DE" />
            <enumeration value="JP" />
        </restriction>
    </simpleType>
</element>

<element name="Address">
    <complexType>
        <sequence>
            <element name="Name" type="string" minOccurs="0"  />
            <element name="StreeName" type="string" minOccurs="0"  />
            <element name="City" type="string" />
            <element name="PostalCode" type="string" />
            <element name="Country" type="CountryCode" />
        </sequence>
    </complexType>
</element>

</schema>

当我尝试验证此架构时,我收到与CountryCode类型中的Country属性相关的错误,例如

解析组件时出错。检测到“CountryCode”位于名称空间“http://www.w3.org/2001/XMLSchema”中,但此命名空间中的组件无法从模式文档“file:/// C:/Eclipse/workspace/Procurement.xsd”中引用。

我做过一些研究并看过类似的帖子,但仍然无法解决这个问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如果要定义类型,则无法使用<element>。只需定义类型本身:

<simpleType name="CountryCode">
  <restriction base="string">
    <enumeration value="US" />
    <enumeration value="DE" />
    <enumeration value="JP" />
  </restriction>
</simpleType>

在XSD中包含此类型意味着该类型将位于目标命名空间中。因此,您需要引用它:

<element name="Country" type="tns:CountryCode" />

通过这两项修改,您的XSD应该是正确的。

相关问题