Search code examples
xmlxsdxml-parsingwsdl

Restrictions in XSD not working


I've a doubt in XML processing with WSDL and XSDs.

Ex: Below is the field mentioned in XSD

<xs:element minOccurs="0" name="VALDATE">
  <xs:simpleType>
    <xs:restriction base="xs:string">
       <xs:pattern value="\d{1,4}-?/?\d{1,4}-?/?\d{1,4}" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Here, what I've done is I'm expecting to read a date value in string format matching the mentioned pattern.

Problem is, if I pass value 12-12-1212, it will going through and if I pass value 1212:12:12, then also it is getting through. I mean if XML processor is not matching string against the pattern, then what is the meaning of it?

Another example is:

<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>

Now, in this field, if I give more than 10 chars, then also it will go through while it shouldn't.

Shouldn't system throw any error based on parameter mentioned?

Thanks in advance!

//EDIT Starts

The Actual Scenario is: I am expecting date to come in different formats. So, in wsdl file, I mentioned element as date type without any restrictions. Now, when request was coming, if date was in YYYY-MM-DD format, system was accepting otherwise system was neglecting the value and was going further. By further I mean, when xml reached to Java, there was no Tag for date field.

For that, I changed the element data type to String(mentioned above). Now, system is accepting all the values and same is available in Java and there I'm doing pattern matching for that field. System is now working fine.

But why system is not performing XSD validations? Is there any flag?

Also, I generated the Java files with wsdl and xsds using wsimport utility of JDK.

//EDIT Ends


Solution

  • The key to your question is:

    But why system is not performing XSD validations?

    and the answer is, because you haven't asked it to.

    To see where it would be appropriate to request schema validation, we would need to know rather more about the design of your application.