我如何拥有一个具有*简单内容*的 XSD 复杂类型,它对枚举值和扩展有限制

How do I have a XSD complextype with *simple content* that has a restriction with enum values AND an extension

那么,我如何才能拥有具有 简单内容 且对枚举值有限制的 XSD 复杂类型?

(!) 没有额外的简单类型:

所以...有点像这样:-- 只工作 ;) (请注意,这是一个简化的示例。请参阅我喜欢实现的 xml)

        <element name="question">
            <complexType>
                <simpleContent>
                            <enumeration value="no"></enumeration>
                            <enumeration value="maybe"></enumeration>
                            <enumeration value="yes"></enumeration>
                            <xs:attribute name="name" type="xs:string" />
                    </extension>
                </simpleContent>
            </complexType>
        </element>

--

最后这是 xml 的模拟,我有:

<question name="foo">
    yes
</question>

参考:http://www.w3schools.com/schema/el_simpleContent.asp

我认为以下是您的想法。创建一个限制允许值的简单类型,以 xs:string 为基础。然后,在 complexType 定义中扩展这个新的、用户定义的简单类型。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="question" type="questionType"/>

    <xs:complexType name="questionType" mixed="true">
        <xs:simpleContent>
            <xs:extension base="enumStringType">
                <xs:attribute name="name" type="xs:string">
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:simpleType name="enumStringType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="no"></xs:enumeration>
            <xs:enumeration value="maybe"></xs:enumeration>
            <xs:enumeration value="yes"></xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

注意:这只会验证以下文档:

<question name="foo">yes</question>

question 的文本内容不包含空格。要忽略任何前导或尾随空格或空格字符序列,您必须将 whiteSpace 方面添加到限制中:

<xs:whiteSpace value="collapse"/>