XSD Simple Element

XML Schema can define elements of XML files.

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

What are simple elements?

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

However, the constraint "contains only text" can be easily misunderstood. Text can have many types. It can be one of the types included in the XML Schema definition (boolean, string, data, etc.), or it can be a custom type you define yourself.

You can also add constraints (i.e., facets) to the data type to limit its content, or you can require that the data matches a specific pattern.

Defining simple elements

Syntax for defining simple elements:

<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>Smith</lastname>
<age>28</age>
<dateborn>1980-03-27</dateborn>

This is the corresponding definition of simple elements:

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

Default values and fixed values of simple elements

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

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

In the following examples, the default value is "red":

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

Fixed values are also automatically assigned to elements, and you cannot specify another value.

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

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