XML模式,为什么xs:group不能是xs:all的子级?



根据本页(以及我的实践),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内部。但我想定义一个模式,其中两个元素(上例中的firstlast)都存在或都不存在,所以我把它们组成一个组。然后我想让组成为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:groupxs:sequencexs:choice

XML模式1.1允许xs:all:下的xs:elementxs:anyxs: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中)要求firstlast"两者都存在或都不存在",如下所示:

<?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>

最新更新