Question

In all of the examples that I've seen using <xs:choice> when defining schema, the elements are always simple elements, which may reference some external type, but never have sub-elements of their own. Is this a rule, or a convention? For example, is it possible to do this?

<xs:element name="shape">
  <xs:complexType>
    <xs:choice>
      <xs:element name="circle">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="radius" type="xs:decimal" />
            <xs:element name="x" type="xs:integer" />
            <xs:element name="y" type="xs:integer" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="rectangle">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="length" type="xs:integer" />
            <xs:element name="width" type="xs:integer" />
            <xs:element name="x" type="xs:integer" />
            <xs:element name="y" type="xs:integer" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:choice>
  </xs:complexType>
</xs:element>

Which would be roughly the equivalent of writing

data Shape = Circle Double Int Int
           | Rectangle Int Int Int Int

in Haskell.

Was it helpful?

Solution

Yes, it's possible to do this. I've only checked it "by eye" but it looks OK to me.

I don't know Haskell, but I don't think the effect is the same. In your XSD you've defined Shape as an element that contains either a Circle or a Rectangle as a child element. If you want to define Circle and Rectangle as being substitutable for Shape (which sounds a more plausible design), then you should:

  • define Shape with abstract="true"
  • define Circle with substitutionGroup="Shape"
  • define Rectangle with substitutionGroup="Shape"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top