问题
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>
回答1:
Most likely you are using a schema processor that doesn't support XSD 1.1.
回答2:
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 typeYearType
to the element, butYearType
is not derived fromxs:string
, the declared type ofproperty
, so it's not legal as a type alternative. Since two of your alternatives are derived fromxs:string
and one fromxs:gYear
, you need to specify something more general thanxs:string
as the declared type ofproperty
. Any ofxs:anyType
,xs:anySimpleType
, orxs:anyAtomicType
should do.
来源:https://stackoverflow.com/questions/14593284/how-to-use-alternatives-in-xml-schema-1-1