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