当另一个元素具有特定值时,在XSD中需要XML元素吗?

时间:2016-05-06 11:24:49

标签: xml xsd xsd-validation xml-validation xsd-1.1

仅当选择了枚举的特定值时,我才需要必需的属性或元素。示例如下:

  <xs:element name="TYPE" type="TestEnum" />
   <!-- // This Element should only required when TYPE = INTERNATIONAL -->
   <xs:element name="IBAN"/>

 </xs:complexType>
<xs:simpleType name="TestEnum">
    <xs:restriction base="xs:string">
        <xs:enumeration  value="NATIONAL"/>
        <xs:enumeration value="INTERNATIONAL"/>
    </xs:restriction>
</xs:simpleType>

1 个答案:

答案 0 :(得分:3)

XSD 1.1

以下是在xs:assert时使用IBAN强制TYPE = 'INTERNATIONAL'的必要性:

<?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="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="TYPE" type="TestEnum" />
        <!-- // This Element should only required when TYPE = INTERNATIONAL -->
        <xs:element name="IBAN" minOccurs="0"/>        
      </xs:sequence>
      <xs:assert test="not(TYPE = 'INTERNATIONAL') or IBAN"/>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="TestEnum">
    <xs:restriction base="xs:string">
      <xs:enumeration  value="NATIONAL"/>
      <xs:enumeration value="INTERNATIONAL"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>