Creating an XML document with more than 1 child element with XML schema

半世苍凉 提交于 2020-01-05 09:23:44

问题


I have come with an xml schema to create an xml document. The root node is movies. Now this root element has child elements called movie which contains other elements of a string type and every movie also has about three or four attributes. However after linking my XML document with the schema I am only able to create ONE movie node and no more than that. Here is my xml schema:

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--root element movies --> 
<xsd:element name="movies">
    <xsd:complexType >
        <xsd:all >
            <xsd:element name="movie">
                <xsd:complexType >
                    <xsd:sequence>
                        <!--Elements under each movie-->
                        <xsd:element name="title" type="xsd:string"/>
                        <xsd:element name="writer" type="xsd:string"/>
                        <xsd:element name="producer" type="xsd:string"/>
                        <xsd:element name="director" type="xsd:string"/>
                        <!--Not sure the number of actors a movie can have -->
                        <xsd:element name="actor" type="xsd:string" maxOccurs="unbounded"/>
                        <xsd:element name="poster" type="xsd:string"/>
                        <xsd:element name="comments" type="xsd:string"/>
                    </xsd:sequence>
                    <xsd:attribute name="type" use="required">
                        <!--attribute type with its options -->
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:enumeration value="drama"/>
                                <xsd:enumeration value="comedy"/>
                                <xsd:enumeration value="adventure"/>
                                <xsd:enumeration value="sci-fi"/>
                                <xsd:enumeration value="mystery"/>
                                <xsd:enumeration value="horror"/>
                                <xsd:enumeration value="romance"/>
                                <xsd:enumeration value="documentary"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:attribute>
                    <xsd:attribute name="rating" use="required">
                        <!--attribute rating with its options -->
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:enumeration value="G"/>
                                <xsd:enumeration value="PG"/>
                                <xsd:enumeration value="PG-13"/>
                                <xsd:enumeration value="X"/>
                                <xsd:enumeration value="ua"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:attribute>
                    <xsd:attribute name="review" use="optional">
                        <!--attribute review with its options -->
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:enumeration value="1"/>
                                <xsd:enumeration value="2"/>
                                <xsd:enumeration value="3"/>
                                <xsd:enumeration value="4"/>
                                <xsd:enumeration value="5"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:attribute>
                    <xsd:attribute name="year" use="optional">
                        <!--attribute year -->
                    </xsd:attribute>
                </xsd:complexType>
            </xsd:element>
        </xsd:all>
    </xsd:complexType>
</xsd:element>

Can someone help finding my mistake?


回答1:


In order to allow a sequence of more than one movie within movies:

(1) Change xsd:all to xsd:sequence.

(2) Change

    <xsd:element name="movie">

to

    <xsd:element name="movie" maxOccurs="unbounded">


来源:https://stackoverflow.com/questions/25409242/creating-an-xml-document-with-more-than-1-child-element-with-xml-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!