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

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

本文介绍了基于 XSD 中的属性枚举控制元素基数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 cell @type 是 filled, numbered, specialNumberedspecialUnnumbered.如果 cell @type 是 multiCharacterNumberedmultiCharacterUnnumbered,我希望 character 元素不可重复.

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.

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

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

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

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>

推荐答案

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

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>

这篇关于基于 XSD 中的属性枚举控制元素基数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:04