XML Schema Tuto...login
XML Schema Tutorial
author:php.cn  update time:2022-04-20 14:13:02

XSD simple elements



XML Schema defines the elements of an XML file.

Simple elements refer to those elements that only contain text. It will not contain any other elements or attributes.


What are simple elements?

Simple elements refer to those elements that only contain text. It will not contain any other elements or attributes.

However, the "only text" limitation can easily lead to misunderstandings. There 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 add qualifications (i.e. facets) to a data type to limit its content, or you can require the data to match a specific pattern.


Define simple elements

The 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>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"/>


Default and fixed values ​​of simple elements

Simple elements can have specified Default or fixed value.

The default value is automatically assigned to the element when no other value is specified.

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" />