向 XML 文件添加元素

Adding elements to the XML file

我需要向此 XML 文件添加更多 "Artwork" 元素,并使用相同的标题、媒体、描述、创建和显示 child 元素,我尝试使用 max0ccurs=unbounded但看到我只能在元素与全局元素不直接相关时使用它。 这是 XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="Artworks">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Artwork"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Artwork">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Title" type="xs:string"/>
                <xs:element name="Media" type="xs:string"/>
                <xs:element name="Description" type="xs:string"/>
                <xs:element name="Created" type="xs:string"/>
                <xs:element name="Display" type="xs:string"/>

            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这是XML

<Artworks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="artwork1.xsd">
    <Artwork>
        <Title>Xtreme Air</Title>
        <Media>Glass Sculpture</Media>
        <Description>An amazing work that uses glass balloon shaps to illustrate a rainbow of balloons circuling a glass earth.</Description>
        <Created>April 2010</Created>
        <Display>Orlando Museum of Arts</Display>
        <Artwork></Artwork>

   </Artwork>

</Artworks>

您必须在第一个模板中的元素用法定义中添加 maxOccurs="unbounded"

<xs:element name="Artworks">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Artwork" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
...

现在您的 Artworks 元素中可以有无限的 Artwork 个子元素(但不是递归的)。