Require XML element in XSD when another element has certain value?

我只是一个虾纸丫 提交于 2019-11-26 04:27:42

问题


I need a required attribute or element only if a specific value of an enumeration is chosen. Example below:

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


XSD 1.1

Here's how use xs:assert to make IBAN be mandatory when 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>


来源:https://stackoverflow.com/questions/37071177/require-xml-element-in-xsd-when-another-element-has-certain-value

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