命名空间组件不可引用

Namespace components are not referenceable

下面是我的部分XML架构

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

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

解析组件时出错。检测到 'CountryCode' 在命名空间“http://www.w3.org/2001/XMLSchema”中,但无法从架构文档 'file:///C:/Eclipse/workspace/Procurement.xsd'.

中引用来自此命名空间的组件

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

有什么想法吗?

如果要定义类型,则不能使用<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应该是正确的。