0

Input:

<params>
   <param name ="query" >
    <queryData>
        <criterialist  isCaseSensitive ="true">
            <criteria>
                <condition>is</condition>
                <value>2</value>
                <attributeName>ActivityVersionId</attributeName>
            </criteria>
        </criterialist>
       </queryData>
   </param>
</params>

In the above input the criterialist is optional. So I can have a input like the below as well.

<params>
  <param name ="query" >
    <queryData>
       </queryData>
  </param>
</params>

How can I validate these above input with a single XSD?

The XSD which I am using now is:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="params" type="paramsType"/>
  <xs:complexType name="criteriaType">
    <xs:sequence>
      <xs:element type="xs:string" name="condition"/>
      <xs:element type="xs:byte" name="value"/>
      <xs:element type="xs:string" name="attributeName"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="criterialistType">
    <xs:sequence>
      <xs:element type="criteriaType" name="criteria"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="isCaseSensitive"/>
  </xs:complexType>
  <xs:complexType name="queryDataType">
    <xs:sequence>
      <xs:element type="criterialistType" name="criterialist"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="paramType">
    <xs:sequence>
      <xs:element type="queryDataType" name="queryData"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="name"/>
  </xs:complexType>
  <xs:complexType name="paramsType">
    <xs:sequence>
      <xs:element type="paramType" name="param"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

This validates the first input, but not the second one.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Sammy
  • 121
  • 4
  • 12
  • optional is minoccurs -> http://www.w3schools.com/xml/schema_complex_indicators.asp – Allan S. Hansen Dec 14 '15 at 16:33
  • Possible duplicate of [How to make an element in XML schema optional?](http://stackoverflow.com/questions/9243772/how-to-make-an-element-in-xml-schema-optional) – kjhughes Dec 14 '15 at 16:49

1 Answers1

3

As already pointed out in a comment, use minOccurs="0":

<xs:complexType name="queryDataType">
    <xs:sequence>
        <xs:element type="criterialistType" name="criterialist" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

With this modification, you will be able to validate a document without a criterialist element:

<params>
    <param name ="query" >
        <queryData>
        </queryData>
    </param>
</params>

But a document with more than one criteria element inside criterialist:

<params>
    <param name ="query" >
        <queryData>
            <criterialist  isCaseSensitive ="true">
                <criteria>
                    <condition>is</condition>
                    <value>2</value>
                    <attributeName>ActivityVersionId</attributeName>
                </criteria>
                <criteria>
                    <condition>is not</condition>
                    <value>4</value>
                    <attributeName>SomethingElse</attributeName>
                </criteria>
            </criterialist>
        </queryData>
    </param>
</params>

will still not be allowed. In my opinion, "list" suggests you also intended to allow the following, with maxOccurs:

<xs:complexType name="criterialistType">
    <xs:sequence>
        <xs:element type="criteriaType" name="criteria" maxOccurs="unbounded"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="isCaseSensitive"/>
</xs:complexType>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
  • I tried with `minOccurs="0"` before posting it here, but it didn't worked that time. Not sure why? And now it is working! – Sammy Dec 14 '15 at 16:53
  • @Sammy Perhaps you added `minOccurs` in the wrong place in the XSD, or forgot to save, or forgot to validate again? What tool are you using for the validation? – Mathias Müller Dec 14 '15 at 16:54
  • 3
    @Sammy, glad to hear you resolved your problem. Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) Mathias' answer if it's helped you. Thanks. – kjhughes Dec 14 '15 at 17:11