user650900
user650900

Reputation: 73

XSD Choice between three elements

I have three elements A, B & C. I wanted to create an XSD for which the schema should be the choice of [A] or [B & C] or [A & B & C]

Can anyone please help me to create an xsd for the above option.

Thanks in Advance. MK

Upvotes: 2

Views: 2657

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

This satisfies your request; thinking why you might have asked, it was probably related to the Unique particle attribution; the optional sequence below acts as a "virtual" choice.

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root">
        <xsd:complexType>
            <xsd:choice>
                <xsd:sequence>
                    <xsd:element name="A"/>
                    <xsd:sequence minOccurs="0">
                        <xsd:element name="B"/>
                        <xsd:element name="C"/>
                    </xsd:sequence>
                </xsd:sequence>
                <xsd:sequence>
                    <xsd:element name="B"/>
                    <xsd:element name="C"/>
                </xsd:sequence>
            </xsd:choice>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

XSD Diagram

Upvotes: 1

Drona
Drona

Reputation: 7234

You can create three separate groups of elements like [A], [B&C] and [A,B & C]. And define a compleType with choice between these groups something like the piece of XSD. Not sure if its validates. I dont have a XSD authoring tool to verify if it does.

<xs:group name="Group1">
    <xs:sequence>
        <xs:element name="A"/>
    </xs:sequence>
</xs:group>

<xs:group name="Group2">
    <xs:sequence>
        <xs:element name="B"/>
        <xs:element name="C"/>
    </xs:sequence>
</xs:group>

<xs:group name="Group3">
    <xs:sequence>
        <xs:element name="A"/>
        <xs:element name="B"/>
        <xs:element name="C"/>
    </xs:sequence>
</xs:group>

<xs:complexType name="choice1">
    <xs:choice minOccurs="1" maxOccurs="1">
      <xs:group ref="Group1" />
      <xs:group ref="Group2"/>
      <xs:group ref="Group3"/>
    </xs:choice>
</xs:complexType>

Upvotes: 2

Related Questions