scottmgerstl
scottmgerstl

Reputation: 765

How to use alternatives in XML Schema 1.1

From everything I have read, the schema I have defined below should work (emphasis on alternatives). I get the following error: The '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 schema:

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

Upvotes: 2

Views: 8095

Answers (2)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

As Michael Kay has already pointed out, the most likely cause of your error message is that your unidentified XSD processor doesn't support XSD 1.1.

But there are a other few issues that will cause problems for any conforming XSD 1.1 processor.

  • An XSD 1.1 processor will reject the attribute-value specification mixed="true" on any complex type with simple content. (A 1.0 processor may issue a warning, but otherwise 1.0 says just to ignore it.)

  • The second alternative in the element declaration for property assigns the type YearType to the element, but YearType is not derived from xs:string, the declared type of property, so it's not legal as a type alternative. 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 as the declared type of property. Any of xs:anyType, xs:anySimpleType, or xs:anyAtomicType should do.

Upvotes: 2

Michael Kay
Michael Kay

Reputation: 163272

Most likely you are using a schema processor that doesn't support XSD 1.1.

Upvotes: 1

Related Questions