XSD Simple Elements

We can use the XML Schemas to define the elements of the XML files. An XML element that contains only text is called a simple element. No other elements or attributes can be included in it.

What is a Simple Element?

An XML element that contains only text is called a simple element and no other elements or attributes can be included in it. The “only text” restriction of a simple element is misleading though. There are many different types of texts, such as the types included in the XML Schema definition (boolean, string, date, etc.), or a custom type. Also, to limit the contents of a data type, we can add restrictions (facets), or the data can be required to match a specific pattern.

Defining a Simple Element:

Syntax: Defining a simple element:

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

Here, the name of the element is represented by xxx and the data type of the element is represented by yyy.

XML Schema built-in data types:

The most common built-in data types of an XML Schema are:

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

Example:

XML elements:

<firstname>Tom</firstname>
<age>25</age>
<birthdate>2000-09-21</birthdate>

Corresponding simple element definitions:

<xs:element name="firstname" type="xs:string"></xs:element>
<xs:element name="age" type="xs:integer"></xs:element>
<xs:element name="birthdate" type="xs:date"></xs:element>

Default and Fixed Values for Simple Elements:

We can either specify a default value or a fixed value for a simple element. When no other value is specified, a default value is automatically assigned to the element.

Example:

<xs:element name="flower" type="xs:string" default="rose"></xs:element>

Explanation:
Here, the default value is “rose”.
When a fixed value is assigned, we cannot specify another value. It is also automatically assigned to the element.

Example:

<xs:element name="flower" type="xs:string" fixed="rose"></xs:element>

Explanation:
Here, the fixed value is “rose”.

Please Share