6

给定以下示例:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Book" abstract="true">
        <xs:sequence>
            <xs:element name="titel" type="xs:string">
            </xs:element>
            <xs:element name="bookCode" type="BookEnum"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Lyric">
        <xs:complexContent>
            <xs:extension base="Book">
                <xs:sequence>
                    <xs:element name="author" type="xs:string">
                    </xs:element>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:simpleType name="BookEnum">
        <xs:restriction base="xs:int">
            <xs:enumeration value="Paperback"/>
            <xs:enumeration value="Hardcover"/>
            <xs:enumeration value="Liporello"/>
            <xs:enumeration value="None"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

歌词来源于书。我想将复杂类型“Lyric”的 BookEnum 的可能值限制为“Paperback”。

“None”、“Liporello”和“Hardcover”不应再是“Lyric”的有效值。这可以在xsd中完成吗?

4

2 回答 2

9

不,不幸的是你不能这样做。没有办法限制这样的枚举,而且您在同时扩展和限制复杂类型时也会遇到麻烦。有一篇很好的文章 what you can do with enumerations here

也许考虑自下而上地工作:定义一个基本的书籍类型,其中几乎没有任何内容;然后定义一个 LyricType,它是基本类型和更多值的联合;在子类型中携带 bookCode。这并不理想,但是很遗憾,XSD 并不总是与面向对象的语言保持一致。

于 2010-02-15T15:54:33.703 回答
1

最终..您正在尝试使用另一个节点(父级或兄弟级等)的值来验证一个节点..这当然是不可能的..

于 2010-02-15T16:41:49.287 回答