- If I delete the version attribute, it validates,
As it should, because when no xs:alternative
tests match, person
can be xs:anyType
per Common Mapping Rules for Element Declarations.
- If I replace the version 1 by 3, it validates,
As it should for the same reason as #1.
- If I delete all the content of person, it validates.
No, it doesn't. You may not have truly associated the XML document with your XSD, or you may have accidently combined experiment #3 with experiments #1 or #2.
Updated XSD
The following updated XSD specifies a default type of person
rather than xs:anyType
to avoid the surprises #1 and #2. Note that it also uses extension to consolidate common parts of the declarations of person_v1
and person_v2
into person_v
and to satisfy the requirement that the alternative types must be derived from a common base class. (When unspecified, it's xs:anyType
and typically goes unnoticed.)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:complexType name="person_v">
<xs:sequence>
<xs:element name="firstname" type="xs:token"/>
<xs:element name="lastname" type="xs:token"/>
</xs:sequence>
<xs:attribute name="version" type="xs:token" use="required"/>
</xs:complexType>
<xs:complexType name="person_v1">
<xs:complexContent>
<xs:extension base="person_v"/>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="person_v2">
<xs:complexContent>
<xs:extension base="person_v">
<xs:sequence>
<xs:element name="birthdate" type="xs:date"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="person" type="person_v">
<xs:alternative test="@version=1" type="person_v1"/>
<xs:alternative test="@version=2" type="person_v2"/>
</xs:element>
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element ref="person" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Update: Here's an update to person_v
to require that version be 1
or 2
to address a concern in the comments:
<xs:complexType name="person_v">
<xs:sequence>
<xs:element name="firstname" type="xs:token"/>
<xs:element name="lastname" type="xs:token"/>
</xs:sequence>
<xs:attribute name="version" use="required">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…