0

I need to validate the following element:-

 <xs:element name="IsBucketRequired" type="xs:unsignedInt"/>

Validation required:-

  1. Allow 1,0 [Pattern]
  2. Allow Empty value.

I try with following code:-

    <xs:element name="IsBucketRequired" nillable="true"  minOccurs="1">
      <xs:simpleType>
        <xs:restriction base="xs:unsignedInt">
          <xs:pattern value="[1,0]"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>

Above method validate 1,0 correctly but it's not allow Empty value.

another try for same validation but not working.

  <xs:element name="IsBucketRequired">
      <xs:simpleType>
        <xs:union>
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="0"/>
            </xs:restriction>
          </xs:simpleType>
          <xs:simpleType>
            <xs:restriction base="xs:unsignedInt">
              <xs:pattern value="[1,0]"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:union>
      </xs:simpleType>
    </xs:element>

Anyone please tell me how to write validation code for my requirement.

Thank you

Gurunathan
  • 97
  • 4
  • 19

1 Answers1

0

Well, your first attempt (nillabel="true") should work. You just need to express that element in XML instance is empty (="null") by xsi:nil="true" attribute.

<IsBucketRequired xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />

Alternatively you can define your simple type as a restriction of xs:string.

<xs:simpleType name="zeroOrOneOrEmpty">
    <xs:restriction base="xs:string">
        <xs:enumeration value="" />
        <xs:enumeration value="1" />
        <xs:enumeration value="0" />
    </xs:restriction>
</xs:simpleType>

Third option could be definition using union like

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:simpleType name="zeroOrOne">
        <xs:restriction base="xs:unsignedInt">
            <xs:enumeration value="0" />
            <xs:enumeration value="1" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="emptyString">
        <xs:restriction base="xs:string">
            <xs:length value="0" />
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="zeroOrOneOrEmpty">
        <xs:union memberTypes="zeroOrOne emptyString" />
    </xs:simpleType>

    <xs:element name="IsBucketRequired" type="zeroOrOneOrEmpty" />

</xs:schema>
Jirka Š.
  • 3,388
  • 2
  • 15
  • 17