XML validation for accepting Null value aswell as Time (HH:MM)

Last Reply one year ago By born2achie

Posted one year ago

Hi,

below are the sample to play with

 

<?xml version="1.0" encoding="utf-8"?>
<UsersList>
  <User>
    <Name>sam</Name>
    <BirthTime></BirthTime>
  </User>
  <User>
    <Name>wit</Name>
    <BirthTime></BirthTime>
  </User>
</UsersList>

 

<?xml version="1.0" encoding="UTF-8"?>
	<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
	<xs:element name="UsersList">
    <xs:complexType> 	  
	  <xs:sequence>    
        <xs:element ref="User" maxOccurs="unbounded"/>
		</xs:sequence>
    </xs:complexType>
  </xs:element>
		
	  <xs:element name="User">
		<xs:complexType>
		  <xs:sequence>
			<xs:element ref="Name" />
			<xs:element ref="BirthTime" />
			</xs:sequence>
    </xs:complexType>
	  </xs:element>
	 
 
	  <xs:element name = "Name">
	   <xs:simpleType>
		  <xs:restriction base = "xs:string">
			 <xs:pattern value="[a-zA-Z0-9]*"/>
		  </xs:restriction>
	   </xs:simpleType>
	</xs:element>

	  <xs:element name="BirthTime">
		<xs:simpleType>
		  <xs:restriction base="xs:string">
			<xs:pattern value="(0[0-9]|[1][0-9]|2[0-3]):[0-5][0-9]"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  
	</xs:schema>

Basically i want the birth time should allow  empty value or value with HH:MM format. for HH:MM format i managed but unable to allow the emoty value. tried with nillable = true option. but not working. any sugestion please

Posted one year ago

Hi born2achie,

Please refer below XML Validation you need to use union for restriction with length value='0' so that it can allow only null values and as well as your HH:MM format also.

XML

<xs:element name="BirthTime" >
  <xs:simpleType>
    <xs:union>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:length value='0'/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="(0[0-9]|[1][0-9]|2[0-3]):[0-5][0-9]"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:element>

Posted one year ago

I tried the way mentieond in below link and it worked.

http://www.w3schools.com/xml/el_union.asp

 

By the way thanks for pointing out the union keyword. great help

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html