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

Dai*_*jan 1 xml enums xsd

那么,如何拥有一个具有简单内容且具有枚举值限制的 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>
Run Code Online (Sandbox Code Playgroud)

--

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

<question name="foo">
    yes
</question>
Run Code Online (Sandbox Code Playgroud)

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

Mat*_*ler 5

我认为以下内容就是您的想法。创建一个简单类型,以限制允许的值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>
Run Code Online (Sandbox Code Playgroud)

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

<question name="foo">yes</question>
Run Code Online (Sandbox Code Playgroud)

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

<xs:whiteSpace value="collapse"/>
Run Code Online (Sandbox Code Playgroud)