How to use alternatives in XML Schema 1.1

From everything I've read, the schema defined below (emphasis on alternatives) should be defined. I am getting the following error: http://www.w3.org/2001/XMLSchema:alternative ' element is not supported in this context.

Could you please point out what I did wrong?

Here is my current schematic:

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

      

+1


source to share


2 answers


You are most likely using a schema processor that does not support XSD 1.1.



0


source


As Michael Kay pointed out, the most likely reason for your error message is that your unidentified XSD processor does not support XSD 1.1.

But there are other issues that can cause problems for any applicable XSD 1.1 processor.



  • The XSD 1.1 processor will reject the attribute specification mixed="true"

    for any complex type with simple content. (A 1.0 processor may issue a warning, but otherwise 1.0 says to just ignore it.)

  • The second alternative in an element declaration for property

    assigns an element to a type YearType

    , but is YearType

    not derived from the xs:string

    declared type property

    , so it is not an alternative to the type. Since two of your alternatives are derived from xs:string

    and one from xs:gYear

    , you need to specify something more general than xs:string

    the declared type property

    . Any of xs:anyType

    , xs:anySimpleType

    or xs:anyAtomicType

    should do.

+1


source







All Articles