简体   繁体   中英

XSD schema for a list of elements containing text and having attributes?

Let's say I have the following 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>

and want to create an XSD schema for it.

I can't get the definition for the <word> elements right for some reason. What I've come up with so far is:

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

but when I run

xmllint -noout -schema wordlist.xsd

I get

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

Line 11 is

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

so it seems the first word works as expected, but not the second one...

Change

<xs:element name="word">

to

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

because the default for maxOccurs is 1 , but you wish to allow multiple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM