Nil Values

Welcome to our free XML tutorial. This tutorial is based on Webucator's Introduction to XML Training course.

Contact Us or call 1-877-932-8228
Nil Values

Nil Values

When an optional element is left out of an XML instance, it has no clear meaning. For example, suppose a schema declares a Name element as having required FirstName and LastName elements and an optional MiddleName element. And suppose a particular instance of this schema does not include the MiddleName element. Does this mean that the instance author did not know the middle name of the person in question or does it mean the person in question has no middle name?

Setting the nillable attribute of xs:element to true indicates that such elements can be set to nil by setting the xsi:nil attribute to true.

Code Sample:

<?xml version="1.0"?>
<xs:schema xmlns:xs="">
	<xs:element name="Author">
				<xs:element name="FirstName" type="xs:string"/>
				<xs:element name="MiddleName" type="xs:string" nillable="true"/>
				<xs:element name="LastName" type="xs:string"/>

Code Sample:

<?xml version="1.0"?>
<Author xmlns:xsi="" 
	<MiddleName xsi:nil="true"/>

By including the MiddleName element and setting xsi:nil to true, we are explicitly stating that we do not know anything about Mark Twain's middle name. He might have had one, but we don't know what it is, and if we do, we're not saying.

This is, of course, somewhat strange. It implies that if we had simply left the MiddleName out, we would be indicating that Mark Twain has no middle name. The important thing is that the application consuming your XML data can differentiate between nil values, empty elements, and missing elements.


This tutorial is based on Webucator's Introduction to XML Training Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.