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

XML Schema attributes


XSD Attributes


All attributes are declared as simple types.


What are attributes?

Simple elements cannot have attributes. If an element has attributes, it is treated as a composite type. But the properties themselves are always declared as simple types.


How to declare attributes?

The syntax for defining attributes is

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

Here , xxx refers to the attribute name, and yyy specifies the data type of the attribute. 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

This is an XML element with attributes:

<lastname lang="EN">Smith< ;/lastname>
This is the corresponding attribute definition:

<xs:attribute name="lang" type="xs:string"/>


Default and fixed values ​​for attributes

Attributes can have specified default or fixed values.

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

In the following example, the default value is "EN":

<xs:attribute name="lang" type="xs:string" default="EN "/>
Fixed values ​​are also automatically assigned to elements, and you cannot specify another value.

In the following example, the fixed value is "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN" />


Optional and required attributes

By default, attributes are optional. To specify that an attribute is required, use the "use" attribute:

<xs:attribute name="lang" type="xs:string" use="required"/>


Qualification of content

When an XML element or attribute has a defined data type, qualification is added to the content of the element or attribute.

If the type of the XML element is "xs:date" and the content it contains is a string similar to "Hello World", the element will not (pass) validation.

With XML schema, you can also add your own restrictions to your XML elements and attributes. These limitations are called facets (Editor's note: means (polyhedron) face, can be translated as limited face). You'll learn more about facets in the next section.