根据本页(以及我的实践),xs:group
元素不能是xs:all
的子元素。所以类似的东西
<xs:group name="g">
<xs:element name="first" type="xs:string"/>
<xs:element name="last" type="xs:string"/>
</xs:group>
<xs:all>
<xs:group ref="g" minOccurs="0" maxOccurs="1"/>
<xs:element name="id" type="xs:string"/>
</xs:all>
无效,因为组不能在xs:all
内部。但我想定义一个模式,其中两个元素(上例中的first
和last
)都存在或都不存在,所以我把它们组成一个组。然后我想让组成为xs:all
的一部分,因为组可以与其他元素(例如上面的id元素)一起以任何顺序出现。换句话说,我希望有几个元素作为一个整体是可选的。如果xs:group
不能成为xs:all
的孩子,我怎么能做到这一点?
XMLSchema1.0仅允许xs:all
下的xs:element
(和xs:annotation
)。
<all
id = ID
maxOccurs = 1 : 1
minOccurs = (0 | 1) : 1
{any attributes with non-schema namespace . . .}>
Content: (annotation?, element*)
</all>
不允许xs:group
、xs:sequence
、xs:choice
等
XML模式1.1允许xs:all
:下的xs:element
、xs:any
或xs:group
<all
id = ID
maxOccurs = (0 | 1) : 1
minOccurs = (0 | 1) : 1
{any attributes with non-schema namespace . . .}>
Content: (annotation?, (element | any | group)*)
</all>
注意:允许无序元素听起来可能很理想,但很少是真正需要的。通常CCD_ 19在实践中就足够了。
如果您愿意放弃无序需求,您可以(甚至在XSD 1.0中)要求first
和last
"两者都存在或都不存在",如下所示:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:sequence minOccurs="0">
<xs:element name="first" type="xs:string"/>
<xs:element name="last" type="xs:string"/>
</xs:sequence>
<xs:element name="id" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>