【发布时间】: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”的真正定义是什么?
它真的只允许使用一个已定义的元素,还是仅将下一个子节点的可能选择限制为已定义的元素?
【问题讨论】: