Reputation: 649
I have a type hierarchy in xsd like the following: Type A -> Type B -> Type C...
Now Type B is defined like:
<xs:complexType name="TypeB">
<xs:complexContent>
<xs:extension base="TypeA" >
<xs:sequence>
...
<xs:element name="ElementA" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Type C does not need to add any elements to Type B so it is defined like:
<xs:complexType name="TypeC">
<xs:complexContent>
<xs:extension base="TypeB">
</xs:extension>
</xs:complexContent>
</xs:complexType>
Now the restrictions for Type C for ElementA have changed - it's maxIncluse should be 5 not 10.
I can change Type C but changing Type B should not break compatibility. Is there a way to change the restrictions without breaking compatibility/the existing hierarchy?
Upvotes: 0
Views: 263
Reputation: 7279
You can do so by declaring TypeC as a restriction of TypeB and explicitly redefining its complex content. However, for it to work, you need to explicitly declare the simple type that restricts xs:int between 0 and 10, and explicitly restrict it to another simple type that changes the maxInclusive to 5. It is important that this simple type restriction is explicit, as explained here.
The schema could look like so:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ElementC" type="TypeC"/>
<xs:complexType name="TypeA">
<xs:sequence>
<xs:element name="foobar" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="TypeB">
<xs:complexContent>
<xs:extension base="TypeA" >
<xs:sequence>
<xs:element name="ElementA" type="Type10" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:simpleType name="Type10">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Type5">
<xs:restriction base="Type10">
<xs:maxInclusive value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="TypeC">
<xs:complexContent>
<xs:restriction base="TypeB">
<xs:sequence>
<xs:element name="foobar" type="xs:string"/>
<xs:element name="ElementA" type="Type5" minOccurs="0"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:schema>
and then this will be valid:
<?xml version="1.0" encoding="UTF-8"?>
<ElementC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<foobar/>
<ElementA>5</ElementA>
</ElementC>
and this not:
<?xml version="1.0" encoding="UTF-8"?>
<ElementC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<foobar/>
<ElementA>6</ElementA>
</ElementC>
Upvotes: 1