Angel Mane
Angel Mane

Reputation: 3

Reuse common elements in XSD

in XSD how can I reuse the tag <indicator> and its common elements like <aa> and <bb>, because there are other elements under it on each Req.

<Req>
 <AReq>
    ...
    <indictator>
       <aa></aa>
       <bb></bb>
       <numero></numero>
    </indicator>
 </AReq>

 <BReq>
    ...
    <indictator>
       <aa></aa>
       <bb></bb>
       <name> </name>
    </indicator>
 </BReq>

 <CReq>
    ...
    <indictator>
       <aa></aa>
       <bb></bb>
       <data> </data>
    </indicator>
 </CReq>
</Req>

Upvotes: 0

Views: 64

Answers (1)

Michael Kay
Michael Kay

Reputation: 163360

There are a number of ways of doing this.

You could give the three indictator elements different types that reuse the declarations of a and b:

<xs:element name="AReq">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="indictator">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="a"/>
            <xs:element ref="b"/>
            <xs:element name="numero">
              ...

with global element declarations for a and b.

Or you could go further and define a named model group (xs:group) containing a and b.

Or you could define the types of the three indictator elements as three different types all derived by extension from a base type that only includes a and b.

In all these approaches the key thing to note is that the three indictator element have different content models, and therefore need to have separate element declarations (which must necessarily be local declarations).

If you have an XSD 1.1 processor there is another option: you could define the type of indictator to allow a choice of numero, name, or data, and define an assertion at the level of the containing element, for example AReq, to the effect that it must have a descendant numero element: <xs:assert test="exists(.//numero)"/>

Upvotes: 1

Related Questions