Detailed explanation of XML Schema full-touch graphic and text code

黄舟
Release: 2017-03-28 16:55:33
Original
2351 people have browsed it

Content summary: XML Schema, like DTD, is responsible for defining and describing the structure and content schema of XML documents. It can define which elements and relationships between elements exist in the XML document, and can define the data types of elements and attributes .

What is XML Schema

XML Schema, like DTD, is responsible for defining and describing the structure and content schema of XML documents. It can define which elements and relationships between elements exist in the XML document, and can define the data types of elements and attributes.

XML Schema itself is an XML document, which conforms to the XML syntax structure. It can be parsed with a common XML parser.

Why use Schema

We have already used DTD to define an XML structure and data type, so why do we need Schema?

Because DTD has many defects :

1) DTD is based on regular expressions and has limited description capabilities;

2) DTD does not have data type support and is insufficient in most application environments ;

3) DTD's constraintsdefinition capabilities are insufficient to make more detailed semantic restrictions on XML instance documents;

4) The structure of DTD is not structured enough and cannot be reused The cost is relatively high;

5) DTD does not use XML as a description method, and there is no standardprogramminginterface for the construction and access of DTD, and standards cannot be used Programmatically perform DTD maintenance.

XML Schema is designed to address the shortcomings of these DTDs. The advantages of XML Schema are:

1) XML Schema is based on XML and has no special syntax

2) XML can be parsed and processed like other XML files

3) XML Schema supports a series of data types (int, float, Boolean, date, etc.)

4) XML Schema provides extensible Data model.

5) XML Schema supports comprehensive namespace

6) XML Schema supports attribute groups.

A simple XML Schema document

Detailed explanation of XML Schema full-touch graphic and text code

#An element is defined in this Schema: quantity, its type is nonNegativeInteger (non-negative integer), xmlns It is the namespace of Schema, which has been described in Part 3 above.

The following XML fragment is legal:

<quantity>5</quantity>
Copy after login

The following XML fragment is illegal:

<quantity>-4</quantiy>
Copy after login

Types in Schema

Schema mainly includes Three components: element, attribute, and notation.

These three basic components can also be combined into the following components:

a) Type definition components: simple types and composite types

b) Assembly components

c) Attribute component

Simple type

Detailed explanation of XML Schema full-touch graphic and text code

XML Schema defines some built-in data types that can be used to describe elements content and attribute values.

If an element only contains numbers, strings or other data, but does not include sub-elements, this is called a simple type.

As shown in the figure, the element quantity is a simple type. Its element content must be a non-negative integer, excluding any attributes and sub-elements.

<quantity>some</quantity>
Copy after login

All built-in simple types

Primitive types

string,boolean,decimal,float,double,duration
datetime,time,date,gYearMonth,gYear,gMonthDay,
dDay,gMonth,hexBinary,base64Binary,any URI,QName
NOTATION
Copy after login

Derived types (base types in brackets)

normalizedString(string),language(tonken),token(normalizedString)
NMTOKEN(token),Name(token),NCName(Name),ID(NCName),IDREF(NCName)
IDREFS(list of IDREF),ENTITY(NCName),ENTITIES(list of ENTITY)
integer(decimal),nonPositiveInteger(integer),
negativeInteger(noPositiveInteger),long(integer),int(long),
short(int),byte(short),nonNegativeInteger(integer)
unsignedLong(nonNegativeInteger),unsignedInt(unsignedLong),
unsignedShort(unsignedInt),unsignedByte(unsignedShort),
positiveInteger(nonNegativeInteger)
Copy after login

Creating simple types

Detailed explanation of XML Schema full-touch graphic and text code

In the figure, we first create a simple type: quantityType, which inherits from integer. minInclusive and maxInclusive define its minimum value 2 and maximum Value 5. Finally, we define the type of element quantity as quantityType.

正确:  <quantity>3</quantity>
错误:  <quantity>10</quantity>
<qauntity>aaa</quantity>
Copy after login

Using restriction, we can limit the acceptance of certain values ​​​​or only certain text,

基本方面:equal,ordered,bounded,cardinality,numeric
限制方面:length,minLength,maxLength
pattern,enumeration
whiteSpace
maxInclusive,maxExclusive,minInclusive,minExclusive
totalDigits,fractionDigits
Copy after login

Simple type example 1

Detailed explanation of XML Schema full-touch graphic and text code

The value of this SKU type: 3 numbers followed by a hyphen followed by two uppercase English letters.

pattern is followed by a regular expression. See other books for regular expression syntax.

正确:  <ourSKU>123-AB</ourSKU>
错误:  <ourSKU>abc-AB</ourSKU>
<ourSKU>123-ab</ourSKU>
Copy after login

Simple type example 2

Detailed explanation of XML Schema full-touch graphic and text code

#This is a type USState used to describe the names of American states. All state names are listed through enumeration. Take When the value is entered, only the state names listed in it can be taken.