Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate empty string value tag in xsd

Tags:

xsd

I have an xml file which is having some date values and other datatypes.

<Purchasedate Name="purcaseDate" value=""/>

I am validating these xml files with a xsd file. In xsd shcema I have written a regular expression pattern for dd/mm/yyyy format.

This is working fine if value attribute have a value. My pattern is validating against the value attribute.

The field (purchasedate) is not mandatory. if value="", this means my pattern is validating against an empty string also, which is not mandatory.

I need to validate the optional field and i am using <xs:attribute name="PurchaseDate" use="optional"> also.

I need to validate this field when value tag is not empty.

like image 742
user467955 Avatar asked Feb 14 '10 10:02

user467955


People also ask

What does minOccurs mean in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is XML validation against XSD?

XML Validator - XSD (XML Schema) XSD files are "XML Schemas" that describe the structure of a XML document. The validator checks for well formedness first, meaning that your XML file must be parsable using a DOM/SAX parser, and only then does it validate your XML against the XML Schema.

What is XSD validation error?

The schema is defined by the XSD. Schema errors occur where there is a problem with the structure or order of the file, or an invalid character is included. Schema errors prevent the validation being run in full because the file cannot be read. This means that errors cannot be traced to a particular record.


1 Answers

That's too easy ..

Just all you have to do is to include empty string specification in your pattern

This is the way to do that .. <xs:pattern value="|(Regular_pattern_goes_here)"/>

For your reference I have written a sample chunks of codes .. just go through them ..

sample XML:

<?xml version="1.0" encoding="utf-8"?>
<xmln xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com XMLFile1.xsd" xmlns="http://www.xsdef.com/xml/123">
  <Purchasedate Name="purcaseDate" value=""/>
</xmln>

sample XSD:(includes custom type def)

<xs:schema xmlns:xsLocal="http://www.xsdef.com/xml/123" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xsdef.com/xml/123" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="xmln">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Purchasedate">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="Name" type="xs:string" use="required" />
                <xs:attribute name="value" type="xsLocal:CUSTOM_DATE" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="CUSTOM_DATE">
    <xs:restriction base="xs:string">
      <xs:pattern value="|((01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)/(01|02|03|04|05|06|07|08|09|10|11|12)/[1-2][0-9][0-9][0-9])"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
like image 161
InfantPro'Aravind' Avatar answered Nov 14 '22 22:11

InfantPro'Aravind'