XSD Simple Elements


XML Schema can define elements of an XML file.

Simple elements refer to those that contain only text. They do not contain any other elements or attributes.


What is a Simple Element?

Simple elements refer to those that contain only text. They do not contain any other elements or attributes.

However, the restriction "contains only text" can easily cause misunderstanding. There are many types of text. It can be one of the types included in the XML Schema definition (boolean, string, data, etc.), or it can be a custom type defined by yourself.

You can also add restrictions (i.e., facets) to data types to restrict their content, or you can require the data to match a specific pattern.


Defining a Simple Element

The syntax for defining a simple element:

<xs:element name="xxx" type="yyy"/>

Here xxx refers to the name of the element, and yyy refers to the data type of the element. XML Schema has many built-in data types.

The most commonly used types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Example

Here are some XML elements:

<lastname>Refsnes</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>

Here are the corresponding simple element definitions:

<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>


Default and Fixed Values for Simple Elements

Simple elements can have a specified default value or fixed value.

When no other value is specified, the default value is automatically assigned to the element.

In the example below, the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element, and you cannot specify another value.

In the example below, the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>
Other Extensions