1

我对具有结构的 XML 文件感兴趣:

<resource>
    <type>STRING</type>
    <metadata>
        <ANY_EXTERNAL_ELEMENT1>
            <value>STRING</value> 
        </ANY_EXTERNAL_ELEMENT1>
        <ANY_EXTERNAL_ELEMENT2>
            <reference>STRING</reference> 
        </ANY_EXTERNAL_ELEMENT2>
        <ANY_EXTERNAL_ELEMENT3>
            <value>STRING</value> 
        </ANY_EXTERNAL_ELEMENT3>
    </metadata>
</resource>

元数据元素需要至少有一个 ANY_EXTERNAL_ELEMENT 子元素,该子元素只需要一个名称在集合 {"reference", "value"} 中的子元素。

是否可以在 XMLSchema 中实现它?

我尝试了什么:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="resource">
        <xs:complexType>
            <xs:all>
                <xs:element name="type" type="xs:string"/>
                <xs:element name="metadata">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:any minOccurs="1">
                                <xs:complexType>
                                    <xs:choice>
                                        <xs:element name="reference"/>
                                        <xs:element name="value"/>
                                    </xs:choice>
                                </xs:complexType>
                            </xs:any>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

但这无效。我需要帮助。

提前致谢。

4

2 回答 2

2

不,您不能像那样约束“外部”元素。<any> 的内容模型只允许 <annotation>。倒置结构不是更自然吗:

<resource>
<type>STRING</type>
<metadata>
    <external value="STRING">
        <ANY-EXTERNAL-ELEMENT-1/>
    </external>
    <external ref="STRING">
        <ANY-EXTERNAL-ELEMENT-2/>
    </external>
</metadata>
</resource>

但是,当然,我不知道您的用例是什么。

于 2011-10-07T12:25:24.257 回答
1

我认为您必须在 xs:choice 中枚举可能的元素(ANY-EXTERNAL-ELEMENT-1 等),或者如果您想包含它们的类型,则通过替换组。

于 2011-10-07T13:58:58.060 回答