2

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Glenn
  • 21
  • 1
  • @PetruGardea: It is my impression some of the reopen reviewers are not reading all the way to the comments on a question to be reopened. Editing the text with the explicit information about why this is different would have helped as such changes are more prominent for the reviewer. – Anthon Apr 07 '13 at 15:02
  • @Anthon, I take your comment at face value, since I really don't know what is the role of a `reopen reviewer` on SO. Initially I checked on meta with regards to comments for reopen, which is where I wanted to put my reasoning - non existing, as explained there... The thing is, there is nothing to add to this question, in my opinion, than what Glenn already put in: he wants an XSD 1.1 solution for co-constraints, period. Linking to an answer that says it is impossible was simply a case of gross misunderstanding. – Petru Gardea Apr 08 '13 at 17:30
  • @Glenn How are and elements related? Did you miss adding within ? – IndoKnight Feb 02 '14 at 12:57

1 Answers1

0

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>

Beth Lang
  • 1,889
  • 17
  • 38