基于 XSD 中的属性枚举控制元素基数?

Control element cardinality based on an attribute enumeration in XSD?

如果 cell @type 是 fillednumberedspecialNumbered 或 [=17=,我希望 character 元素可重复].如果 cell @type 是 multiCharacterNumberedmultiCharacterUnnumbered,我希望 character 元素不可重复。

如果 cell 类型是 blank,我想完全禁止 character 元素。

这需要 XSD 1.1 吗?如何形成条件或测试?

<xs:element name="cell" maxOccurs="unbounded">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="character" type="xs:string" minOccurs="0"/>
         </xs:sequence>
         <xs:attribute name="type" use="required">
              <xs:simpleType>
                   <xs:restriction base="xs:string">
                        <xs:enumeration value="filled"/>
                        <xs:enumeration value="blank"/>
                        <xs:enumeration value="numbered"/>
                        <xs:enumeration value="specialNumbered"/>
                        <xs:enumeration value="specialUnnumbered"/>
                        <xs:enumeration value="multiCharacterNumbered"/>
                        <xs:enumeration value="multiCharacterUnnumbered"/>
                   </xs:restriction>
               </xs:simpleType>
          </xs:attribute>
      </xs:complexType>
  </xs:element>

是的,XSD 1.1(不是XSD 1.0)可以表达你的复杂约束:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="cell">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="character" type="xs:string" 
                    minOccurs="0"  maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:attribute name="type" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="filled"/>
            <xs:enumeration value="blank"/>
            <xs:enumeration value="numbered"/>
            <xs:enumeration value="specialNumbered"/>
            <xs:enumeration value="specialUnnumbered"/>
            <xs:enumeration value="multiCharacterNumbered"/>
            <xs:enumeration value="multiCharacterUnnumbered"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
      <xs:assert test="(   (@type = 'blank' and count(character) = 0)
                        or (@type = ('multiCharacterNumbered', 'numbered',
                                     'specialNumbered', 'specialUnnumbered') 
                            and count(character) = (0,1))
                        or (@type = ('filled', 'multiCharacterUnnumbered')))"/>
    </xs:complexType>
  </xs:element>
</xs:schema>