【问题标题】:xs:choice - one and only one of the following nodes or one or more of the following nodes?xs:choice - 一个且仅一个以下节点或一个或多个以下节点?
【发布时间】:2013-06-07 16:00:01
【问题描述】:

几年前,我为我的 XML 安装程序文件定义了一个 XSD。 目标是定义一个需求节点,其中包含具有不同名称的子节点,例如:

<requirements>
    <core version="1.0.0.1749" />
    <field version="1.2">chbxgroup</field>
    <php version="5.3"/>
</requirements>

这部分的 XSD 如下所示:

<xs:element name="requirements" minOccurs="0" maxOccurs="unbounded">
    <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="core" type="requirement" minOccurs="0" maxOccurs="1" />
            <xs:element name="cms" type="requirement" minOccurs="0" maxOccurs="unbounded" />
            <xs:element name="plugin" type="requirement" minOccurs="0" maxOccurs="unbounded" />
            <xs:element name="application" type="requirement" minOccurs="0" maxOccurs="unbounded" />
            <xs:element name="field" type="requirement" minOccurs="0" maxOccurs="unbounded" />
            <xs:element name="php" type="requirement" minOccurs="0" maxOccurs="unbounded" />
        </xs:choice>
    </xs:complexType>
</xs:element>

以及需求定义:

<xs:complexType name="requirement" mixed="true">
    <xs:attribute name="version" use="optional">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="(\d+\.)?(\d+\.)?(\d+)?(\.)?(\d+)?" />
                <xs:whiteSpace value="preserve" />
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="type" use="optional">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="function|class" />
                <xs:whiteSpace value="preserve" />
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

如果您愿意,可以查看完整的定义:https://xml.sigsiu.net/SobiPro/application.xsd

现在重要的信息是:它工作得非常好,完全符合我们的要求

问题是我们有一个非常好奇和友善的用户,他检查了定义,他正在尝试了解 XML 和 XSD,他将我指向http://www.w3schools.com/Schema/el_choice.asp 的文档,其中清楚地指出您“xs:选择”实际上只允许使用定义的子节点之一。

我正在检查不同的文档,实际上每个文档都说明完全相同。 因此,为了我理解我们定义的方式以及我了解到的方式,它不应该真正起作用。 但确实如此

一些例子: http://msdn.microsoft.com/en-us/library/ms256109.aspx

允许一个且仅一个包含在所选元素中的元素 包含在包含元素中的组。

http://www.w3schools.com/schema/schema_complex_indicators.asp

指示符指定一个子元素或 另一个可能发生:

那么“xs:choice”的真正定义是什么?

它真的只允许使用一个已定义的元素,还是仅将下一个子节点的可能选择限制为已定义的元素?

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    好的。现在,可以将这些点连接起来了。

    &lt;requirements&gt; 元素的架构是您的 XML 的正确架构。 根据它,确实,在xs:choice 中定义的所有这些孩子 可以在任何配置中重复任意次数。

    那是因为你有 无限 xs:choice:

    <xs:choice minOccurs="0" maxOccurs="unbounded">
    

    这里,出现属性minOccurs="0" maxOccurs="unbounded"说 这个xs:choice 可以重复多次(从 0 到无限)。 因此,xs:choice 中指定的任何内容都可以重复任意次数或相互混合,没有限制。

    关于 "xs:choice" 的含义...是的,xs:choice(这是它的一个实例)只允许使用该选项中定义的元素中的一个元素。更准确地说,xs:choice 只允许您使用其中定义的东西中的一种。那些“东西”(称为“粒子”)实际上不仅是元素, 还有其他选择、序列、组。所以,你可以用所有这些来构建相当复杂的限制。

    【讨论】:

    • 抱歉,我好像粘贴了两次相同的代码 - 我现在才更正它。希望它更好地理解:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2022-10-13
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多