【问题标题】:Adding elements to the XML file将元素添加到 XML 文件
【发布时间】:2020-08-02 08:17:00
【问题描述】:

我需要在这个 XML 文件中添加更多具有相同标题、媒体、描述、创建和显示子元素的“艺术品”元素,我尝试使用 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>

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

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

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

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

    【讨论】:

    • 我之前这样做过...但我只是再次看了一下我将新元素放在第一个关闭 xml 标记之前...但无论如何非常感谢我猜有点沮丧
    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多