本文介绍了带有可选子元素的默认 XML 序列(或全部)必须至少有一个子元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人能确认以下模式的解释是否正确,我将不胜感激:

I would be grateful if someone could confirm if the interpretation of the following schema is correct:

<xs:element name="Element1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Child1" minOccurs="0" />
            <xs:element name="Child2" minOccurs="0" /> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

虽然Child1Child2 都是可选的,但Element1 必须至少有一个子节点才能符合上述模式;即文档:

Although both Child1 and Child2 are optional, Element1 has to have at least one child to comply with the above schema; i.e. the document:

<Element1></Element1>    

不会遵守.为了使其有效,它需要序列 minOccurs = 0(?)

Would not comply. For this to be valid, it would require sequence minOccurs = 0(?)

更新

当子元素是可选的时,问题涉及序列(和所有)出现的含义.例如文档;

The question relates to the meaning of occurrence for sequence (and all) when child elements are optional. For example, the document;

<Element1>
    <Child2/>
    <Child1/>
</Element1>

将符合上述架构.该序列将发生两次;在第一遍中,Child1 缺席.在第二个中,Child2 缺席.但关键是,序列 minOccurs(默认为 1)是满足的,因为它发生了两次.

Would comply with the above schema. The sequence would have occurred twice; in the first pass, Child1 was absent. In the second, Child2 was absent. But the point is, the sequence minOccurs (default of 1) was satisfied, because it occurred twice.

对于我上面给出的第一个例子(只有 Element1;没有子元素),序列根本不会出现,并且不 (IMO) 满足 minOccurs = 1.

With the first example I gave above (just Element1; no child elements), sequence does not occur at all, and does not (IMO) satisfy minOccurs = 1.

推荐答案

没有...

虽然 Child1Child2 都是可选的,Element1 必须在至少有一个孩子符合上述模式

minOccurs 的默认值为 1,因此您可以正确地假设 xsd:sequence 被限制为出现一次.但是,只要满足其子项的出现约束一次,就满足 xsd:sequence minOccurs="1".当所有子出现约束为 minOccurs="0" 时,允许一个空序列.因此,即使没有任何 Child1Child2 子元素, 也是有效的.

The default value for minOccurs is 1, so you're correct to assume that the xsd:sequence is constrained to appear once. However, xsd:sequence minOccurs="1" is satisfied as long as its children's occurrence constraints are satisfied once. When all child occurrence constraints are minOccurs="0", one empty sequence is allowed. Therefore, <Element1/> is valid, even without any Child1 or Child2 children elements.

另见

带有 xs:sequence minOccurs="0"

<xs:element name="r">
    <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="1">
            <xs:element name="a"/>
            <xs:element name="b"/> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

有效的 XML: ;

Valid XML: <r/> and <r><a/><b/></r>

带有 xs:sequence minOccurs="1" 的 XSD(默认)

XSD with xs:sequence minOccurs="1" (default)

<xs:element name="r">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="a"/>
            <xs:element name="b"/> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

有效的 XML: <r><a/><b/></r>

带有 xs:sequence minOccurs="2"

<xs:element name="r">
    <xs:complexType>
        <xs:sequence minOccurs="2" maxOccurs="2">
            <xs:element name="a"/>
            <xs:element name="b"/> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

有效的 XML: <r><a/><b/><a/><b/></r>

这篇关于带有可选子元素的默认 XML 序列(或全部)必须至少有一个子元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:52