tags:

views:

26

answers:

2

Hi,

I need to develop an xsd for the scenario. where i have 2 element of types Server1 and Server2. There can be any number of occurances for Server1 and Server2 but atleast one of the occurance is mandatory either Server1 or Server2.

<element name="Server1">
  <complexType>
   <sequence>
    <element name="hostName" type="string"/>
    <element name="portNumber" type="integer"/>
    <element name="userName" type="string"/>
   </sequence>
  </complexType>
</element>
<element name="Server2">
  <complexType>
   <sequence>
    <element name="hostName" type="string"/>
    <element name="portNumber" type="integer"/>
   </sequence>
  </complexType>
</element>

Thanks Ravi

+3  A: 

You can wrap them to choice schema element with maxOccurs attribute set to unbounded.

Sample:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <element name="root">
        <complexType>
            <choice maxOccurs="unbounded">
                <element name="Server1">
                    <complexType>
                        <sequence>
                            <element name="hostName" type="string"/>
                            <element name="portNumber" type="integer"/>
                            <element name="userName" type="string"/>
                        </sequence>
                    </complexType>
                </element>
                <element name="Server2">
                    <complexType>
                        <sequence>
                            <element name="hostName" type="string"/>
                            <element name="portNumber" type="integer"/>
                        </sequence>
                    </complexType>
                </element>
            </choice>
        </complexType>
    </element>
</schema>
Matej
It's probably better to use references rather than nested elements (it makes maintenance easier in the long run) but it's definitely the right approach
Nic Gibson
Thanks Matej, the solution provided by you works.
ravi
A: 

I'm not sure it's the best possibility by any means, but one possibility would be for Server1 and Server2 to be just type names, and then create an element that's a union of Server1 and Server2.

<xsd:comlexType name=Server1>
   <sequence>
   // ...
   </sequence>
</xsd:complextype>

<xsd:complexType name=Server2>
// ...
</xsd:complexType>

<element name="Server">
   <xsd:union memberTypes = "Server1 Server2" />
</element>
Jerry Coffin