2

从我读过的所有内容来看,我在下面定义的模式应该可以工作(强调替代方案)。我收到以下错误:此上下文不支持“ http://www.w3.org/2001/XMLSchema:alternative ”元素。

你能指出我做错了什么吗?

这是我当前的架构:

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

  <xs:element name="object">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded" minOccurs="0">
        <xs:element name="property" type="xs:string">
          <xs:alternative test="@name='VIN'" type="VinType"/>
          <xs:alternative test="@name='Year'" type="YearType"/>
          <xs:alternative test="@name='Make'" type="MakeType"/>
        </xs:element>
      </xs:choice>
      <xs:attribute name="type" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>

  <!-- Vehicle Identification number (VIN) -->
  <xs:simpleType name="VinRestriction">
    <xs:restriction base="xs:string">
      <xs:length fixed="true" value="17"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="VinType" mixed="true">
    <xs:simpleContent>
      <xs:extension base="VinRestriction">
        <xs:attribute name="name" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="VIN" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>


  <!-- Vehicle Year -->
  <xs:simpleType name="YearRestriction">
    <xs:restriction base="xs:gYear"/>
  </xs:simpleType>

  <xs:complexType name="YearType" mixed="true">
    <xs:simpleContent>
      <xs:extension base="YearRestriction">
        <xs:attribute name="name" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="Year" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <!-- Vehicle Make -->
  <xs:simpleType name="MakeRestriction">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Chevrolet"/>
      <xs:enumeration value="Ford"/>
      <xs:enumeration value="Mazda"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="MakeType" mixed="true">
    <xs:simpleContent>
      <xs:extension base="MakeRestriction">
        <xs:attribute name="name" use="required">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="Make" />
            </xs:restriction>
          </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>
4

2 回答 2

2

正如 Michael Kay 已经指出的那样,您的错误消息最可能的原因是您的未识别 XSD 处理器不支持 XSD 1.1。

但是还有其他一些问题会导致任何符合 XSD 1.1 的处理器出现问题。

  • XSD 1.1 处理器将拒绝mixed="true"任何具有简单内容的复杂类型的属性值规范。(1.0 处理器可能会发出警告,但 1.0 表示只是忽略它。)

  • 元素声明中的第二个替代方案property将类型分配给YearType元素,但YearType不是从xs:string的声明类型派生的property,因此作为替代类型是不合法的。由于您的两个替代方案是派生自xs:string一个,一个是从派生的xs:gYear,因此您需要指定比xs:string声明的类型更通用的东西property。任何一个xs:anyTypexs:anySimpleTypexs:anyAtomicType应该做。

于 2013-01-30T16:56:13.950 回答
1

您很可能正在使用不支持 XSD 1.1 的模式处理器。

于 2013-01-30T08:32:31.307 回答