Simple Type of Schema syntax

Source: Internet
Author: User

First of all, there are three simple types in Schema: simpleelement, attribute, and restrictions ). The following describes these types one by one.

I. XSD simpleelement

Simpleelement refers to an element that only contains text (content between elements) without any other elements and attributes. Here, text refers not only to text.
(String). To be precise, it should be data. It can be data of the built-in data type in the schema, or data of the Data Type we have created.

Define a simpleelement

<Xs: element name = "XXX" type = "yyy">

Here, we will remind you again that XML is case sensitive and that attribute values should be enclosed in double quotation marks. "XXX" is the name of the element to be defined. "Yyy" is the data type of this element. The predefined built-in data types of XML Schema are as follows:
"XS: string", "XS: decimal", "XS: integer", "XS: Boolean", "XS: Date", and "XS: Time ".
Let's look at an example. The following are some simpleelements in XML.

<Lastname> refsnes </lastname>
<Age> 36 </age>
<Dateborn> 1970-03-27 </dateborn>

The following definition should be made in the schema. Note that the element name corresponds to the data type.

<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 Element

The default value is the default value provided when no value is specified in elment. It is given using the default attribute.

<Xs: element name = "color" type = "XS: string" default = "red"/>

The fixed value is also automatically given, but XML users cannot specify a value for element and use the fixed attribute.

<Xs: element name = "color" type = "XS: string" fixed = "red"/> 2. XSD attribute

All attributes are of the simple type. A simpleelement cannot have attributes. If an element contains attriute, we assume that
It is regarded as a complex type (complex type ). Although attribute will not appear separately, we will introduce it as a simpletype here

Define an attribute

<Xs: attribute name = "XXX" type = "yyy"/>

"XXX"
Is the name of attribute. "Yyy" is the data type of attribute. You can use the built-in data types defined in XML schema, for example, "XS: String
"," XS: decimal "," XS: integer "," XS: Boolean "," XS: Date ", and" XS: Time ".
For example, the following element contains an attribute.

<Lastname lang = "en"> Smith </lastname>

The schema statement that defines the lang attribute should be as follows:

<Xs: attribute name = "Lang" type = "XS: string"/>

Default and fixed values of attribute

You can also use the default and fixed attributes to specify the default and fixed values for attrbute.
Default Value

<Xs: attribute name = "Lang" type = "XS: string" default = "en"/>

Fixed value

<Xs: attribute name = "Lang" type = "XS: string" fixed = "en"/>

Optional and required attributes

The attribute is optional by default (that is, you can leave it empty). You can use the use attribute to specify the attribute, as shown below:

<Xs: attribute name = "Lang" type = "XS: string" use = "required"/> 3. XSD restrictions (constraints)

Restrictions is used to limit (or define) acceptable values of element or attribute. Element restrictions are usually called facets.
The following describes the usage and syntax of restrictions through some common constraints.

Numeric range limit

<Xs: element name = "Age"> <Xs: simpletype>
<Xs: Restriction base = "XS: integer">
<Xs: minequalsive value = "0"/>
<Xs: maxcompute sive value = "120"/>
</Xs: Restriction>
</Xs: simpletype> </Xs: Element>

This constraint defines that the value of the age element is an integer and the value must be between 0 and 120.

Enumeration restrictions

<Xs: element name = "car">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: enumeration value = "Audi"/>
<Xs: enumeration value = "golf"/>
<Xs: enumeration value = "BMW"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>

Here, the car value is a string and can only be one of "Audi", "golf", and "BMW.
You can use another method:

<Xs: element name = "car" type = "cartype"/>
<Xs: simpletype name = "cartype">
<Xs: Restriction base = "XS: string">
<Xs: enumeration value = "Audi"/>
<Xs: enumeration value = "golf"/>
<Xs: enumeration value = "BMW"/>
</Xs: Restriction>
</Xs: simpletype>

The advantage of this writing method is that the above restriction is not defined in the element and can be conveniently called by other elements.

Use regularexpression Constraints

<Xs: element name = "letter">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: Pattern value = "[A-Z]"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>

Here, the value attribute of <Xs: Pattern> is a regular expression, and the syntax is not covered in this article. You can use regularexpression to specify string constraints in any format.

Whitespace characters Constraints

<Xs: element name = "Address">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: whitespace value = "preserve"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>

In the preceding example, all space characters in the address are retained. The key is value = "preserve ". The XML syntax is reserved with spaces.
When the value is "replace"
XML processer replaces all space characters.
When the value is "collapse"
Combine consecutive spaces into one.

Length constraints

<Xs: element name = "password">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: length value = "8"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: Element>

The above example limits the length of the password element to 8. Of course, you can also use <Xs: minlength value = "? "/> And <Xs: maxlength value = "? "/> To limit the maximum and minimum values.

For more information about constraint labels, refer to the following website:
Http://www.w3schools.com/schema/schema_elements_ref.asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.