Co-Occurrence Constraint

社会主义新天地 提交于 2019-12-12 17:20:37

问题


I am having difficulty writing an XSD I'm working on using XML Schema 1.1.

I have one element named PaymentMethod that is either "C" or "F": If PaymentMethod = "C", then it's a check. If PaymentMethod = "F", then it's a fund transfer.

How do I make BankingInfo (BankName, TransitNo, AccountNo, AccountType) optional for a check and mandatory for a fund transfer?

See below for a snippet of my code.


<xs:element name="PaymentMethod">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="C" />
      <xs:enumeration value="F" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

    <xs:complexType name="BankingInfo" minOccurs="0">
      <xs:sequence>
        <xs:element name="TransitNo" type="xs:string" />
        <xs:element name="AccountNo" type="xs:string" />
        <xs:element name="AccountType">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="CHK" />
              <xs:enumeration value="SAV" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="BankName" type="xs:string" />
      </xs:sequence>
    </xs:complexType>

回答1:


You will need to add an assert at the same level as the BankingInfo node because asserts apply at the same level or lower level but can't navigate up to a parent level. The assert test should check the value of account type as well as ensuring the fields that you want to be "required" actually have values.

Either

<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and count(/root/BankingInfo) > 0)"/>

Or, if you want some bank info to be required but not all the fields you could do something like this

<xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and string-length(/root/BankingInfo/TransitNo) > 0 and string-length(/root/BankingInfo/AccountNo) > 0)"/>

Both of the above assume some structure similar to the below (the provided sections aren't a complete enough example to put the assert in at the right level)

<?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" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="PaymentMethod">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="C"/>
                        <xs:enumeration value="F"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="BankingInfo" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="TransitNo" type="xs:string"/>
                        <xs:element name="AccountNo" type="xs:string"/>
                        <xs:element name="AccountType">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="CHK"/>
                                    <xs:enumeration value="SAV"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="BankName" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:assert test="/root/PaymentMethod eq 'C' or (/root/PaymentMethod eq 'F' and count(/root/BankingInfo) > 0)"/>
    </xs:complexType>
</xs:element>



来源:https://stackoverflow.com/questions/13634243/co-occurrence-constraint

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