开发者

Defining recursive algebraic data types in XML XSD

开发者 https://www.devze.com 2022-12-24 18:28 出处:网络
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr = Zero One Add Expr Expr

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 n开发者_如何转开发ame="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!


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号