tags:

views:

748

answers:

2

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

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

and i am validating these xml file with xsd file.I have xsd. In shcema i have written reglarexpression pattern for dd/mm/yyyy format.

this is working fine if value attribute having some values.. my pattes is validating against the value attribute.

the field(purchasedate) is not mandatory to store in DB. if value="" means my pattern is validating against empty string also,which is not mandatory.

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

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

A: 

Hi try adding this attribute nillable="true" into the xml tag definition Also you can take a look at his link http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_over_st0.html
Best Reagds,
Iordan

IordanTanev
A: 

That's too easy @jinagavenkat ..

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"&gt;
  <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>
infant programmer