XSD Simple Element
- Previous Page XSD <schema>
- Next Page XSD Attributes
XML Schema can define elements of an XML file.
Simple elements refer to elements that only contain text. They do not contain any other elements or attributes.
What are simple elements?
Simple elements refer to elements that only contain text. They do not contain any other elements or attributes.
However, the constraint 'only contains text' can easily cause misunderstandings. 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 data types to limit its content, or you can require data to match a specific pattern.
Define simple elements
Syntax for defining simple elements:
<xs:element name="xxx" type="yyy"/>
Here, xxx refers to the element name, and yyy refers to the element data type. 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 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 values and fixed values of simple elements
Simple elements can have specified default values or fixed values.
The default value will be 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"/>
The fixed value will also be automatically assigned to the element, and you cannot specify another value.
In the following example, the fixed value is "red":
<xs:element name="color" type="xs:string" fixed="red"/>
- Previous Page XSD <schema>
- Next Page XSD Attributes