Question

is it possible to override the minOccurs/maxOccurs attributes of an element in the parent xsd? we can easily extend the parent xsd and create a new xsd with different namespace, but am trying to override with same parent namespace.

Lets say we have an xsd

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust"  elementFormDefault="qualified"  attributeFormDefault="unqualified">
<xs:complexType name="customer-type">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="hobby" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>

<xs:element name="customer" type="cust:customer-type"/>
</xs:schema>

can i create extension/restriction of above xsd with same namespace which will restrict/change the <cust:hobby> minOccurs to be 1?

Was it helpful?

Solution

Okay, i was complicating the solution. Before this i was trying to use override function in xml 1.0 which is available only from xml 1.1.

A simple redefine would do the trick:

If the name of your base xsd is cust.xsd then the below redefinition overrides the minOccurs of "hobby" element to 1.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust"  elementFormDefault="qualified"  attributeFormDefault="unqualified">
<xs:redefine schemaLocation="cust.xsd">
<xs:complexType name="customer-type">
<xs:complexContent>
 <xs:restriction base="cust:customer-type">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element name="hobby" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
 </xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top