Control element cardinality based on an attribute enumeration in XSD?

馋奶兔 提交于 2019-12-11 14:02:45

问题


I want the character element to be repeatable if the cell @type is filled, numbered, specialNumbered, or specialUnnumbered. I want the character element NOT to be repeatable if the cell @type is multiCharacterNumbered or multiCharacterUnnumbered.

If the cell type is blank, I'd like to disallow the character element altogether.

Does this require XSD 1.1? How do I form the conditional or test?

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

回答1:


Yes, XSD 1.1 (not XSD 1.0) can express your complex constraint:

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


来源:https://stackoverflow.com/questions/37127791/control-element-cardinality-based-on-an-attribute-enumeration-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!