Welcome to our free XML Schema tutorial. This tutorial is based on Webucator's Introduction to XML Schema course.
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.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ---- C O D E O M I T T E D ---- <xs:element name="Employee"> <xs:complexType> <xs:sequence> <xs:element name="Salary" type="Salary"/> <xs:element name="Title" type="JobTitle" default="Salesperson"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
When defaults are set in the XML schema, the following rules apply for the instance document.
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.
<?xml version="1.0"?> <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="EmployeeDefault.xsd"> <Salary>90000</Salary> <Title/> </Employee>
This tutorial is based on Webucator's Introduction to XML Schema Course. We also offer many other XML Training courses. Sign up today to get help from a live instructor.