Alvaro Hernandorena
Alvaro Hernandorena

Reputation: 610

Is this an incorrect use of xsd:choice?

I am writing a program in JavaScript that reads an XSD, creates an HTML form, and exports the form to XML.

And in order to do that I had to learn the structure of XSD but I have a doubt about xsd:choice.

In the example XSD file that I have been given I have this:

<xsd:choice>
    <xsd:sequence>
        <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:choice>
            <xsd:sequence>
                <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
            </xsd:sequence>
            <xsd:sequence>
                <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
            </xsd:sequence>
        </xsd:choice>
    </xsd:sequence>
</xsd:choice>

I don't know if I don't understand the xsd:choice correctly or this code is using nested xsd:choice without any reason.

Wouldn't the above code be exactly the same as:

<xsd:choice>
    <xsd:sequence>
        <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
    </xsd:sequence>
</xsd:choice>

Upvotes: 1

Views: 46

Answers (1)

kjhughes
kjhughes

Reputation: 111491

Title question: No, neither uses of xsd:choice are incorrect – they're just more complex than they have to be.

(And, yes, those two xsd:choice constructs would be the same.)

Both would also be equivalent to this:

  <xsd:choice>
    <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
    <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
  </xsd:choice>

Upvotes: 1

Related Questions