Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Element Not Null or Empty Constraint For Xml?

Tags:

xml

xsd

This is my sample XML Code:

<bestContact> <firstName><![CDATA[12345]]></firstName> <lastName /> </bestContact> 

I am using:

<xs:element name="lastName" type="xs:string" minOccurs="1" nillable="false"/> 

The XSD Should be validate lastName as not null or empty.

like image 425
Ramakrishnan Avatar asked Dec 10 '10 11:12

Ramakrishnan


People also ask

Can XML have NULL values?

In an XML document, the usual way to represent a null value is to leave the element or attribute empty. Some business messages use a special value to represent a null value: <price>-999</price> .

How do I allow null values in XSD?

Answers. The proper way with the W3C XML schema language is using nillable="true" in the schema and xsi: nil="true" in the XML instance document e.g.

What does Nillable mean in XML?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.


1 Answers

Try

<xs:element name="lastName" minOccurs="1" nillable="false">   <xs:simpleType>      <xs:restriction base="xs:string">        <xs:minLength value="1"/>      </xs:restriction>   </xs:simpleType> </xs:element> 
like image 138
Kamal Avatar answered Sep 23 '22 03:09

Kamal