Validate multiple element and value combinations in XML Schema

718 views Asked by At

I am trying to create an XML Schema 1.0 that validates multiple element combinations.

For example,

Is it possible to create a list of possible element - value combination in schema? Like, Valid only if

  1. A=1 && B=2

  2. A=2 && B=4

Pass Example

<Full>  
  <A>1</A>
  <B>2</B>      
</Full>  

<Full>
 <A>2</A>
 <B>4</B>
</Full>

Fail Example

<Full>  
  <A>2</A>
  <B>2</B>      
</Full>  

<Full>
 <A>1</A>
 <B>4</B>
</Full>  

I can't use XML Schema 1.1 yet. Is there any good way to put these logic in one schema?

Sorry about the confusion, I just rewrite my problem again.

1

There are 1 answers

6
Siva Charan On

Here Set below things based on your requirement:-

minOccurs="0" - for zero occurrances or more than that

minOccurs="1" - for minimum one occurrance or more than that

maxOccurs="unbounded" : Set this if you want to use infinite times

maxOccurs="1" : Set this if you want to only for one time

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="FULL">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" type="xs:int" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="B" type="xs:int" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element name="C" type="xs:int" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

UPDATED:

Assuming that your are passing all the three separate XMLs, (Not as one whole XML)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="FULL">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" type="xs:int" minOccurs="1" maxOccurs="1"/>
                <xs:element name="B" type="xs:int" minOccurs="0" maxOccurs="1"/>
                <xs:element name="C" type="xs:int" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Another Update:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="FULL">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" minOccurs="1" maxOccurs="1">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:pattern value="[1-2]"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="B" minOccurs="0">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:pattern value="2"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="C" type="xs:int" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>