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>
精彩评论