The writing of XML Schema

Source: Internet
Author: User

The purpose of XML schema

1. Define what elements are in an XML document

2. Define what attributes will be in an XML document

3. Defines what child nodes are in a node, how many subnodes can be, and the order in which child nodes appear

4. Defines the data type of an element or attribute

5. Defines the default or fixed value of an element or attribute

the root element of the XML schema:

<?xml version= "1.0"?>

<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema" means that the data type is defined from the

Targetnamespace= "Http://www.w3schools.com" indicates what namespace the elements in the document are to be defined from

Xmlns= "Http://www.w3schools.com" indicates what the default namespace for this document is

elementformdefault= "Qualified" > indicates that each element of the XML document is required to have a namespace designation

...... Define the body part ...

</xs:schema>

How to define a simple element

<xs:element here means to define an element

Name= "Color" means the name of the element to be defined

Type= "xs:string" means the data type of the element to be defined

default= "Red" means the default value of the defined element

fixed= "Red"/> means to define a fixed value for the element, which can only take "red" values

The above defines a simple element, an element instance:<color>red</color>

How to define a property

<xs:attribute

Name= "Birthday" indicates the name of the property to be defined

Type= "Xs:date" indicates the data type of the attribute to be defined

default= "2001-01-11" means to define the default value of the property

fixed= "2001-01-11" means to define a fixed value for the property

use= "Required"/> Indicates whether this property must be specified, that is, if you do not specify the schema, the default is not use= "required" property to indicate that the property is optional

How to define restrictions on element or attribute values

1. Maximum minimum value limit

<xs:element name= "Age" >

<xs:simpleType>

<xs:restriction base= "Xs:integer" >

<xs:mininclusive value= "0"/> is greater than or equal to 0,<xs:minexclusive> represents the minimum but does not include the specified value

<xs:maxinclusive value= "/>" is less than or equal to 120,<xs:maxexclusive>

</xs:restriction>

</xs:simpleType>

</xs:element>

2. Enumeration limit, which is a value that can only be taken in a specified number of values

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

3. Pattern restriction, which means that the format of a string must meet the established pattern of matching

Example

Description

<xs:element name= "Letter" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "[A-z]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that only one value can be taken in lowercase letters

<xs:element name= "initials" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "[a-z][a-z][a-z]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates a must be three uppercase letters

<xs:element name= "initials" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "[a-za-z][a-za-z][a-za-z]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that it must be three letters, which can be in uppercase or lowercase

<xs:element name= "Choice" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "[xyz]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that it must be one of the XYZ

<xs:element name= "ProdID" >

<xs:simpleType>

<xs:restriction base= "Xs:integer" >

<xs:pattern value= "[0-9][0-9][0-9][0-9][0-9]"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that the range of numbers is 0-99999

<xs:element name= "Letter" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "([A-z]) *"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Represents a sequence that must be 0 or more lowercase characters

<xs:element name= "Letter" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "([a-z][a-z]) +"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that there must be more than one letter.

<xs:element name= "Gender" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "Male|female"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

To represent a male or a female.

<xs:element name= "Password" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:pattern value= "[A-za-z0-9]{8}"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

Indicates that a 8 alphanumeric character must be

4. Limitation of string length

<xs:element name= "Password" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:length value= "8"/>

</xs:restriction>

</xs:simpleType>

</xs:element>

The length must be 8.

<xs:element name= "Password" >

<xs:simpleType>

<xs:restriction base= "Xs:string" >

<xs:minlength value= "5"/>

<xs:maxlength value= "8"/>

</xs:restriction>

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.