XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

Overriding Element Type to Empty Content - xsi:nil

This section describes a tutorial example on how to override an element content type to be empty using 'xsi:nil'.

Test 1. With the schema, order.xsd, we created in the previous section, we can easily write an XML document, order.xml, that conforms to order.xsd:

<?xml version="1.0"?>
<order>
 <product>XML Schema Tutorial Book</product>
 <price>9.99 Dollars</price>
 <shipDate carrier="FedEx">2007-01-01</shipDate>
</order>

Notice that we provided the "date" content for "shipDate" as required by the schema. order.xml will pass the schema validation as shown below:

>java XsdSchemaValidator order.xsd order.xml

Passed.

Test 2. Now let's try to write an XML document, order_nil.xml, that overrides the "shipDate" element to empty content with "xsi:nil="true"":

<?xml version="1.0"?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <product>XML Schema Tutorial Book</product>
 <price>9.99 Dollars</price>
 <shipDate carrier="FedEx" xsi:nil="true"/>
</order>

Notice that we did not provide the "date" content for "shipDate" as required by the schema. But we specified the special attribute "xsi:nil="true"" to override the "shipDateType" definition. order_nil.xml will pass the schema validation too:

>java XsdSchemaValidator order.xsd order_nil.xml

Passed.

Test 3. But if you don't follow those rules mentioned in the previous section, you will get validation errors. Here is a sample XML document, order_nil_error.xml, that uses "xsi:nil" incorrectly:

<?xml version="1.0"?>
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <product>XML Schema Tutorial Book</product>
 <price xsi:nil="true"/>
 <shipDate carrier="FedEx" xsi:nil="true">2099-12-31</shipDate>
</order>

Here are the XML schema validation errors:

>java XsdSchemaValidator order.xsd order_nil_error.xml

Error:
   Line number: 4
   Column number: 25
   Message: cvc-elt.3.1: Attribute 
   'http://www.w3.org/2001/XMLSchema-instance,nil' must not appear 
   on element 'price', because the {nillable} property of 'price' 
   is false.

Error:
   Line number: 5
   Column number: 64
   Message: cvc-elt.3.2.1: Element 'shipDate' cannot have character
   or element information [children], because 
   'http://www.w3.org/2001/XMLSchema-instance,nil' is specified.

Failed with errors: 2

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

 Miscellaneous Built-in Datatypes

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

Overriding Element Types in XML Documents

 Overriding Element Type to Empty Content - nillable

Overriding Element Type to Empty Content - xsi:nil

 Defining a Derived Datatype from a Base Datatype

 Overriding the Base Datatype - xsi:type

 Overriding the Base Datatype - Errors

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

Overriding Element Type to Empty Content - xsi:nil - Updated in 2014, by Dr. Herong Yang