XML XSD validation - Country and States

65 Views Asked by At

I am new at XML XSD validation. I am trying to validate the counties in the XML as an enumeration list and also the states in each of the countries.

I have this simple XML:

<?xml version="1.0" encoding="UTF-8"?>
<mylist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
    xsi:noNamespaceSchemaLocation="test.xsd">
    <myrow>
        <country>Country 1</country>
        <state>State 11</state>
    </myrow>
    <myrow>
        <country>Country 2</country>
        <state>State 22</state>
    </myrow>
    <myrow>
        <country>Country 3</country>
        <state>State 33</state>
    </myrow>
</mylist>

I am trying to validate the countries and the states in each country as shown in the XSD ...

<?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="mylist">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="myrow" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
        <xs:element name="country" type="country_type" />
        <xs:element name="state" >
          <xs:alternative test="$country eq 'Country 1'" type="country_1_states_type"/>        
          <xs:alternative test="$country eq 'Country 2'" type="country_2_states_type"/>        
          <xs:alternative test="$country eq 'Country 3'" type="country_3_states_type"/>        
        </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
<!-- LookUp Lists -->

<xs:simpleType name="country_type">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Country 1"/>
    <xs:enumeration value="Country 2"/>
    <xs:enumeration value="Country 3"/>
  </xs:restriction>
</xs:simpleType>  

<xs:simpleType name="country_1_states_type">
  <xs:restriction base="xs:string">
    <xs:enumeration value="State 11"/>
    <xs:enumeration value="State 12"/>
    <xs:enumeration value="State 13"/>
  </xs:restriction>
</xs:simpleType>  

<xs:simpleType name="country_2_states_type">
  <xs:restriction base="xs:string">
    <xs:enumeration value="State 21"/>
    <xs:enumeration value="State 22"/>
    <xs:enumeration value="State 23"/>
  </xs:restriction>
</xs:simpleType>  

<xs:simpleType name="country_3_states_type">
  <xs:restriction base="xs:string">
    <xs:enumeration value="State 31"/>
    <xs:enumeration value="State 32"/>
    <xs:enumeration value="State 33"/>
  </xs:restriction>
</xs:simpleType>  

</xs:schema>

The validator threw this error: A Problem Was Found Starting At: Alternative.

Can someone please help?

1

There are 1 best solutions below

1
On

I think it's likely that the error message means your XSD processor only supports XSD 1.0 and not XSD 1.1 (which is required for xs:alternative).

However, your schema wouldn't work anyway. xs:alternative only has access to the element's attributes, not to child elements (and not to variables with a $ sign either, that looks as if you are making random guesses).

This is more of a use case for XSD 1.1 assertions:

test="(Country='A' and State='B') or (Country='X' and State='Y') or ..."