javagirl
javagirl

Reputation: 1675

multiple elements has a lot in common in xsd schema

If I have a lot of complex-types in xsd, which for example has some amount of common fields, like 'id', or 'name', or 'date' or 'anyspecificId', which means that those types quite similar to each other, and it would be nightmare to add again one common field to each of them. Is it possible to define some base type for complex type, so that this complex type would extend fields/items of base parent type? In that case I just add this new field to base type, and not to each of child ones. Is that possible?

Upvotes: 1

Views: 317

Answers (2)

Michael Kay
Michael Kay

Reputation: 163438

Deriving types by extension is certainly one way to tackle this. Another approach which can sometimes work better is to use named model groups. This can be more flexible, for example it doesn't impose the constraint that common elements have to come before custom elements.

Upvotes: 2

andbi
andbi

Reputation: 4454

Of course you should use type extension:

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

<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

</xs:schema>

http://www.w3schools.com/schema/el_extension.asp

Upvotes: 2

Related Questions