views:

16

answers:

1

Imagine I have a recursive algebraic data type like this (Haskell syntax):

data Expr = Zero
          | One
          | Add Expr Expr
          | Mul Expr Expr

I'd like to represent this in XML, and I'd like an XSD schema for it.

I have figured out how to achieve this syntax:

<Expr>
  <Add>
    <Expr>
      <Zero/>
    </Expr>
    <Expr>
      <Mul>
        <Expr>
          <One/>
        </Expr>
        <Expr>
          <Add>
            <Expr>
              <One/>
            </Expr>
            <Expr>
              <One/>
            </Expr>
          </Add>
        </Expr>
      </Mul>
    </Expr>
  </Add>
</Expr>

with this schema:

<xs:complexType name="Expr">
  <xs:choice minOccurs="1" maxOccurs="1">
    <xs:element minOccurs="1" maxOccurs="1" name="Zero" type="Zero" />
    <xs:element minOccurs="1" maxOccurs="1" name="One" type="One" />
    <xs:element minOccurs="1" maxOccurs="1" name="Add" type="Add" />
    <xs:element minOccurs="1" maxOccurs="1" name="Mul" type="Mul" />
  </xs:choice>
</xs:complexType>
<xs:complexType name="Zero">
  <xs:sequence>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="One">
  <xs:sequence>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="Add">
  <xs:sequence>
    <xs:element minOccurs="2" maxOccurs="2" name="Expr" type="Expr" />
  </xs:sequence>
</xs:complexType>
<xs:complexType name="Mul">
  <xs:sequence>
    <xs:element minOccurs="2" maxOccurs="2" name="Expr" type="Expr" />
  </xs:sequence>
</xs:complexType>

But what I really want is this syntax:

<Add>
  <Zero/>
  <Mul>
    <One/>
    <Add>
      <One/>
      <One/>
    </Add>
  </Mul>
</Add>

Is this possible?

Thanks!

+1  A: 

Use substitution groups:

  <xs:element name="Expr" abstract="true"/>

  <xs:element name="Zero" substitutionGroup="Expr">
    <xs:complexType/>
  </xs:element>

  <xs:element name="One" substitutionGroup="Expr">
    <xs:complexType/>
  </xs:element>

  <xs:element name="Add" substitutionGroup="Expr">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="2" maxOccurs="2" ref="Expr" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Mul" substitutionGroup="Expr">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="2" maxOccurs="2" ref="Expr" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
Pavel Minaev
Awesome, thanks for that! I'm kind of an accidental XSD author, so all I know is what I've spotted in Intellisense. :)
Ben Challenor
Now the next question is, can I use substitution groups with the XmlSerializer and inheritance (and some attribute letting it know what derived classes I care about), or do I have to implement IXmlSerializable myself?
Ben Challenor
In answer to my own question, yes. This page is useful: http://richnewman.wordpress.com/2008/01/28/problems-with-using-xsdexe-to-generate-net-classes-from-the-fpml-xsd-schema-introduction-to-using-fpml-with-net-tools-part-4/
Ben Challenor