Skip to main content

XSD Simple Elements

In a XML Schema, a simple element is an XML element that contains only text. It can't contain any other elements or attributes.

What is a Simple Element?

A simple element is an XML element that contains only text. It can't contain any other elements or attributes.

Only text is misleading: the text can be of many different types. It can be one of the types included in the XML Schema definition or it can be a custom type that you can define yourself.

You can also add restrictions (called facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.

Syntax of a Simple Element

You can define a simple element in this way:

<xs:element name="my-name" type="my-type"/>

where

  • my-name is the name of the element
  • my-type is the data type of the element.

XML Schema has a lot of built-in data types. The most common type are:

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

Examples

Simple Element definitions
<xs:element name="lastname" type="xs:string"/>  
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>"
Example
<lastname>Nolan</lastname>  
<age>24</age>
<dateborn>1999-03-28</dateborn>

Default and Fixed Values for Simple Elements

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

A default value is automatically assigned to the element when no other value is specified. For example default="red".

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

A fixed value is also automatically assigned to the element, and you can't specify another value. For example fixed="red".

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