当另一个元素具有特定值时,需要 XSD 中的 XML 元素吗? [英] Require XML element in XSD when another element has certain value?

查看:28
本文介绍了当另一个元素具有特定值时,需要 XSD 中的 XML 元素吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I need a required attribute or element only if a specific value of an enumeration is chosen.下面的例子:

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>

推荐答案

XSD 1.1

TYPE = 'INTERNATIONAL' 时,以下是如何使用 xs:assert 使 IBAN 成为强制性的:

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>

这篇关于当另一个元素具有特定值时,需要 XSD 中的 XML 元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆