Defining recursive algebraic data types in XML XSD

Posted by Ben Challenor on Stack Overflow See other posts from Stack Overflow or by Ben Challenor
Published on 2010-03-24T20:17:51Z Indexed on 2010/03/24 20:33 UTC
Read the original article Hit count: 487

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!

© Stack Overflow or respective owner

Related posts about algebraic-data-types

Related posts about Xml