XSD Error: Type is not declared, or is not a simple type

XSD Error: Type is not declared, or is not a simple type

对于这个解释,我使用了 2 个不同的 XSD:

customEntry.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="customEntry"
    targetNamespace="http://tempuri.org/customEntry.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/customEntry.xsd"
    xmlns:mstns="http://tempuri.org/customEntry.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"        
    >
  <xs:simpleType name="customEntry">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Za-z0-9_%./]*"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

element_ArtStyleSuffix.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="element_ArtStyleSuffix"
    targetNamespace="http://tempuri.org/element_ArtStyleSuffix.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/element_ArtStyleSuffix.xsd"
    xmlns:mstns="http://tempuri.org/element_ArtStyleSuffix.xsd"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
  <xs:import namespace="http://tempuri.org/customEntry.xsd" schemaLocation="customEntry.xsd"/>

  <!-- Civilizations ArtStyleSuffix Enumeration -->
  <xs:simpleType name="enum_ArtStyleSuffix">
    <xs:restriction base="xs:string">
      <xs:enumeration value="_EURO"/>
      <xs:enumeration value="_AFRI"/>
      <xs:enumeration value="_AMER"/>
      <xs:enumeration value="_ASIA"/>
    </xs:restriction>
  </xs:simpleType>

  <!-- ArtStyleSuffix GameData Schema Information -->
  <xs:element name="GameData">
    <xs:complexType>
      <xs:all>
        <xs:element minOccurs="0" maxOccurs="1" name="ArtStyleSuffix">
          <xs:annotation>
            <xs:documentation>
              Select a default ArtStyleSuffix or you may create your own custom one and place its TypeName here.
            </xs:documentation>
          </xs:annotation>
          <xs:simpleType>
            <xs:union memberTypes="customEntry enum_ArtStyleSuffix"/>
          </xs:simpleType>
        </xs:element>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

我的问题出在以下行:

<xs:union memberTypes="customEntry enum_ArtStyleSuffix"/>

Visual Studio 2015 社区抛出错误:

Type 'http://tempuri.org/element_ArtStyleSuffix.xsd:customEntry' is not declared, or is not a simple type.

对我来说它看起来像一个简单的类型,我认为导入行声明了它,所以也许我不完全理解 "import" 因为我没有从导入行收到任何错误,只是联合线。我这样做正确吗?

确实是简单型。但是,它没有在当前XSD的目标命名空间中声明;它在导入的 XSD 的命名空间中声明。在导入的 http://tempuri.org/customEntry.xsd 命名空间中引用 customEntry 以消除错误...

具体来说,在 XSD:

xs:schema 元素上声明一个命名空间前缀
xmlns:ce="http://tempuri.org/customEntry.xsd"

以便您可以在 xs:union 声明中使用它:

<xs:union memberTypes="ce:customEntry enum_ArtStyleSuffix"/>

你的错误就会消失。

旁注:将命名空间命名为 XSD 文件的 URL 是可以接受的,但既不是必需的也不是常规的;考虑删除 .xsd 扩展名。