Default 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
Default Values

Default Values

Elements that do not have any children can have default values. To specify a default value, use the default attribute of the xs:element element.

Code Sample:

<?xml version="1.0"?>
<xs:schema xmlns:xs="">
---- C O D E   O M I T T E D ----

	<xs:element name="Employee">
				<xs:element name="Salary" type="Salary"/>
				<xs:element name="Title" type="JobTitle" default="Salesperson"/>

When defaults are set in the XML schema, the following rules apply for the instance document.

  1. If the element appears in the document with content, the default value is ignored.
  2. If the element appears without content, the default value is applied.
  3. If the element does not appear, the element is left out. In other words, providing a default value does not imply that the element should be inserted if the XML instance author leaves it out.

Examine the following XML instance. The Title element cannot be empty; it requires one of the values from the enumeration defined in the JobTitle simple type. However, in accordance with number 2 above, the schema processor applies the default value of Salesperson to the Title element, so the instance validates successfully.

Code Sample:

<?xml version="1.0"?>
<Employee xmlns:xsi="" 

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.