包含文本和属性的元素列表的XSD架构?

时间:2016-09-08 03:48:49

标签: xml xsd schema xml-validation xmlschemaset

我们说我有以下XML:

<wordlist>
    <language>English</language>
    <author>John Smith</author>
    <words>
        <word id="0">Point</word>
        <word id="1">Triangle</word>
        <word id="2">Apple</word>
        <word id="3">Plate</word>
        <word id="4">Mango</word>
        <word id="5">Computer</word>
    </words>
</wordlist>

并希望为其创建XSD架构。

由于某种原因,我无法获得<word>元素的定义。到目前为止,我提出的是:

<xs:element name="wordlist">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="language" type="xs:string" />
            <xs:element name="author" type="xs:string" />
            <xs:element name="words">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="word">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute name="id" type="xs:integer" use="required"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

但是当我跑步时

xmllint -noout -schema wordlist.xsd

我得到了

dictionary.xml:11: element word: Schemas validity error : Element 'word': This element is not expected.
dictionary.xml fails to validate

第11行

<word id="1">Triangle</word>

所以看起来第一个单词按预期工作,但不是第二个单词......

1 个答案:

答案 0 :(得分:2)

更改

<xs:element name="word">

<xs:element name="word" maxOccurs="unbounded">

因为default for maxOccurs is 1,但您希望允许多个。