【问题标题】:XSD schema for a list of elements containing text and having attributes?包含文本并具有属性的元素列表的 XSD 架构?
【发布时间】:2017-01-15 21:02:23
【问题描述】:

假设我有以下 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 架构。

由于某种原因,我无法正确定义 &lt;word&gt; 元素。到目前为止,我想出的是:

<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>

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

【问题讨论】:

    标签: xml xsd schema xml-validation xmlschemaset


    【解决方案1】:

    改变

    <xs:element name="word">
    

    到

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-30
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多