7
votes

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.

4

4 Answers

9
votes

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>
0
votes

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

0
votes

The '?' character in the Regex means that the character before it must occur 0 or 1 times.

So in order to solve your issue you need to wrap the regex in parenthesis and put a questionmark at the end:

  <xs:simpleType name="PurchaseDateType">
    <xs:restriction base="xs:string">
      <xs:pattern value="(Regular_pattern_goes_here)?"/>
    </xs:restriction>
  </xs:simpleType>

Use this type on your field and you should be fine

0
votes

If you control the syntax of the XML, you should consider defining the element as follows. Since XML-Schema already provides a date type, you should use it unless you have a really good reason. I say this because it will make it easier for others to use the xml and for you to use better code frameworks later. I didn't include the "name" attribute because it seemed redundant to the element name.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:element name="Purchasedate" nillable="true" type="xs:date"/>
<xs:element name="Purchasedate2">
    <xs:complexType>
        <xs:attribute name="value" type="xs:date"/>
    </xs:complexType>
</xs:element>
<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="Purchasedate"/>
            <xs:element minOccurs="0" ref="Purchasedate2"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>