ash
ash

Reputation: 51

XSD - how to add two 'ref' to the same element

I have been trying to form this XSD, can someone help please...

I have an element 'country' as below:

<xs:element name="country">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="id" type="xs:long" minOccurs="0" />
            <xs:element name="isoCode" type="xs:string" minOccurs="0" />
            <xs:element name="currencyCode" type="xs:string" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

Now, I need to form XSD with two elements, 'source country' and 'destination country' which should both reference to 'country'. Can someone please help me to form that XSD.

<xs:element name="crossCountries">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="id" type="xs:long" minOccurs="0" />

            <xs:element ref="country" />   <!-- Source Country -->

            <xs:element ref="country" />   <!-- Destination Country -->

        </xs:sequence>
    </xs:complexType>
</xs:element>

Upvotes: 1

Views: 340

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

You cannot reference an element and assign a different tag name to that reference. What you want to do instead is to define the content model for that element (a complex type would do) and reuse that under differently named tags.

<xs:complexType name="country">
    <xs:sequence>
        <xs:element name="id" type="xs:long" minOccurs="0" />
        <xs:element name="isoCode" type="xs:string" minOccurs="0" />
        <xs:element name="currencyCode" type="xs:string" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

Then:

<xs:element name="crossCountries">
  <xs:complexType>
    <xs:sequence>
        <xs:element name="id" type="xs:long" minOccurs="0" />

        <xs:element name="sourceCountry" type="country" />   <!-- Source Country -->

        <xs:element name="destinationCountry" type="country" />   <!-- Destination Country -->

    </xs:sequence>
  </xs:complexType>
</xs:element>

Upvotes: 2

Related Questions