Nicolas Holthaus
Nicolas Holthaus

Reputation: 8273

remove element from complex type of schema with <xs:redefine>

I have the following schema (simplified to remove types we're not discussing):

baseGUIconfig.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- Types -->
  <xs:complexType name="defaultsType">   
      <xs:sequence>
          <xs:element name="dataDirectory"              type="xs:string"/>
          <xs:element name="UpstreamDataFileExtension"  type="xs:string"/>
          <xs:element name="LocalDataFileExtension"     type="xs:string"/>
          <xs:element name="connectionTimeoutMs"        type="xs:decimal"/>
          <xs:element name="connectionMaxRetry"         type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="os" use="required">
          <xs:simpleType>
              <xs:restriction base="xs:string">
                  <xs:enumeration value="linux"/>
                  <xs:enumeration value="windows"/>
              </xs:restriction> 
          </xs:simpleType>
      </xs:attribute>
  </xs:complexType>
  
  <xs:complexType name="baseGuiConfigType">
      <xs:sequence>
          <xs:element name="defaults" type = "defaultsType" minOccurs="1" maxOccurs="2" />
      </xs:sequence>
  </xs:complexType>

  <!-- Elements -->
  <xs:element name="baseGuiConfig" type="baseGuiConfigType" />

</xs:schema>

However, I have another schema which redefines this one. In it, I would like to remove the UpstreamDataFileExtension element. Is it possible to remove elements, or redefine the defaultsType in such a way that the derivative schema no longer has it?

Here's what I've tried (which doesn't work):

Redefining schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- Base Schema w/ connection restrictions -->
    <xs:redefine schemaLocation="qrc:///schema/baseGUIconfig.xsd">  
        <!-- Redefine the defaultsType to remove Upstream -->
        <xs:complexType name="defaultsType">
            <xs:complexContent>
                <xs:restriction base="defaultsType">
                  <xs:sequence>
                    <xs:element name="dataDirectory"            type="xs:string"/>
                    <xs:element name="LocalDataFileExtension"   type="xs:string"/>
                    <xs:element name="connectionTimeoutMs"      type="xs:decimal"/>
                    <xs:element name="connectionMaxRetry"       type="xs:decimal"/>
                  </xs:sequence>        
                </xs:restriction>
            </xs:complexContent>
        </xs:complexType>
    </xs:redefine>  
</xs:schema>

Upvotes: 1

Views: 2553

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

You can't do it in XSD 1.0 since the element you're trying to remove is mandatory. The restriction mechanism was designed such that the valid XML for the new type must still be valid when matched against the original (restricted) type. The new XSD 1.1 introduced a new construct, override, where one can do whatever one wants.

Upvotes: 2

Related Questions