使用XML模式创建具有多个子元素的XML文档

时间:2014-08-20 15:54:16

标签: xml xsd altova

我已经提供了一个xml架构来创建一个xml文档。根节点是电影。现在,这个根元素具有名为 movie 的子元素,其中包含字符串类型的其他元素,每个影片也有大约三个或四个属性。 然而,在将我的XML文档与模式链接之后,我只能创建一个电影节点,而不是更多。这是我的xml架构:

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

有人可以帮我找错吗?

1 个答案:

答案 0 :(得分:1)

为了允许movie内的多个movies的序列:

(1)将xsd:all更改为xsd:sequence

(2)改变

    <xsd:element name="movie">

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