Home > database >  XSD overriding parent
XSD overriding parent

Time:12-16

I am working with two schema files, parent.xsd and child.xsd which includes the parent schema.

Inside the parent, I have defined a very simple element in the following way

<xs:element name="parentElement">
  <xs:complexType>
    <xs:sequence minOccurs="0" maxOccurs="1">
      <xs:element ref="another_element" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
  <xs:attribute name="firstAttr" type="attrType"/>            
 </xs:complexType>
 </xs:element>

Now, in the child schema I would like to override this element and add a new attribute. The child should have the same name as the parent. Can this be done in XSD?

I have experimented with <xs:extension> but I want to really use the same parent element and not define a new one based on the parent.

CodePudding user response:

First you will need to extract the complex type.

<xs:element name="parentElement" type="typeOfParentElement"/>

<xs:complexType name="typeOfParentElement">
    <xs:sequence minOccurs="0" maxOccurs="1">
      <xs:element ref="another_element" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
  <xs:attribute name="firstAttr" type="attrType"/>            
</xs:complexType>

Then you will need to define a type that extends typeOfParentElement with the additional attribute.

Then you will need to define the type of the child element with this extended type.

It's not clear from your description whether the child element has the same name as the parent element. If it does, you will need to define it in a local element declaration rather than a global element declaration, since you can't have two global elements with the same name.

  • Related