Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XSD Simple Elements


May 28, 2021 XML Schema


Table of contents


XSD Simple Elements

This section explains XML Schema defines simple elements, and from this section you can learn about the definition syntax, common types, and default values of simple elements.

XML Schema defines the elements of an XML file.

Simple elements are those that contain only text. It does not contain any other elements or properties.


What is a simple element?

Simple elements are those that contain only text. It does not contain any other elements or properties.

However, the "text only" qualification is easily misleading. T here are many types of text. It can be one of the types included in the XML Schema definition (boolean, string, data, and so on), or it can be a custom type that you define yourself.

You can also limit the content of a data type by adding a qualification, or facets, or you can ask the data to match a particular pattern.


Define simple elements

Define the syntax of 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 common types are:

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

Instance

Here are some XML elements:

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

This is the corresponding simple element definition:

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


The default and fixed values for simple elements

Simple elements can have a specified default or fixed value.

When no other values are specified, the default values are automatically assigned to the element.

In the following example, 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"/>


Tip: Simple elements cannot have properties. I f an element has a property, it is treated as a composite type. However, the property itself is always declared as a simple type, which is explained in the next section.