Search code examples
xmlxsdxsd-validationxml-validation

Example of extending complex types in XSD?


I am trying to use data within an element without breaking this element existing contract.
Let's simplify my case:

<xs:element name="ExistingContract">
     <xs:complexType>
          <xs:sequence>
               <xs:element name="first" type="FirstType"/>
               <xs:element name="second" type="SecondType"/>
          </xs:sequence> 
      </xs:complexType>
</xs:element>

<xs:element name="NewContract">
     <xs:complexType>
          <xs:sequence>
               <xs:element name="first" type="FirstType"/>
               <xs:element name="second" type="SecondType"/>
               <xs:element name="additionalData" type="AdditionalDataType"/>
          </xs:sequence> 
      </xs:complexType>
</xs:element>

These two inner types are duplicated, and I want to avoid it. Since there isn't any existing wrapping xs:complexType for the inner data, I can take it out from ExistingContract and use it in my NewContract. But then I will break the first contract (which I don't want to).

Are you familiar with any XSD method that I can keep the first contract the same and extract its inner data to my new contract?


Solution

  • You can use xs:extension to extend NewContractType from ExistingContractType:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="ExistingContract" type="ExistingContractType"/>
    
      <xs:complexType name="ExistingContractType">
        <xs:sequence>
          <xs:element name="first" type="FirstType"/>
          <xs:element name="second" type="SecondType"/>
        </xs:sequence> 
      </xs:complexType>
    
      <xs:complexType name="NewContractType">
        <xs:complexContent>
          <xs:extension base="ExistingContractType">
            <xs:sequence>
              <xs:element name="additionalData" type="AdditionalDataType"/>
            </xs:sequence>
          </xs:extension>
        </xs:complexContent>
      </xs:complexType>
    
      <xs:element name="NewContract" type="NewContractType"/>
    
      <xs:complexType name="FirstType"/>
      <xs:complexType name="SecondType"/>
      <xs:complexType name="AdditionalDataType"/>
    
    </xs:schema>