Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use XML Schema

Shulou Source: shulou.com Published: 2022-06-03 12:07:09 09月15日 Update

This article is to share with you about how to use XML Schema. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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 exist in an 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 exist in an XML document, and can define the data types of elements and attributes.

XML Schema itself is an XML document that conforms to the XML grammatical structure. It can be parsed with a generic XML parser.

Why use Schema

We have previously used DTD to define the structure and data type of a XML, so why Schema?

Because DTD has many defects:

1) DTD is based on regular expressions and its description ability is limited.

2) DTD does not support data types and lacks capability in most application environments

3) the constraint definition ability of DTD is insufficient to impose more detailed semantic restrictions on XML instance documents.

4) the structure of DTD is not structured, and the cost of reuse is relatively high.

5) DTD does not use XML as a means of description, and there is no standard programming interface for the construction and access of DTD, so it is impossible to use standard programming methods for DTD maintenance.

And XML Schema is designed to address the shortcomings of these DTD, the advantages of XML Schema:

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 range of data types (int, float, Boolean, date, etc.)

4) XML Schema provides an extensible data model.

5) XML Schema supports integrated namespaces

6) XML Schema supports attribute groups.

A simple XML Schema document

An element is defined in this Schema: quantity, whose type is nonNegativeInteger (non-negative integer), and xmlns is the namespace of Schema, which was described in part 3 earlier.

The following XML snippet is legal:

five

The following XML fragment is illegal:

-4

Types in Schema

There are three main parts in Schema: elements (element), attributes (attribute) and comments (notation).

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

A) Type definition widgets: simple types and compound types

B) Group components

C) attribute group components

Simple type

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

An element that contains only numbers, strings, or other data, but not child elements, is called a simple type.

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

Some

All built-in simple types

Original type

String,boolean,decimal,float,double,durationdatetime,time,date,gYearMonth,gYear,gMonthDay,dDay,gMonth,hexBinary,base64Binary,any URI,QNameNOTATION

Derived type (base type in parentheses)

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), short (nonNegativeInteger) nonNegativeInteger (nonNegativeInteger), nonNegativeInteger (integer), integer (integer), integer (unsignedLong), unsignedLong (unsignedLong)

Create a simple type

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

Correct: 3 error: 10aaa

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

Basic aspects: equal,ordered,bounded,cardinality,numeric restrictions: length,minLength,maxLengthpattern,enumerationwhiteSpacemaxInclusive,maxExclusive,minInclusive,minExclusivetotalDigits,fractionDigits

Example 1 of a simple type

The value of the type of SKU: three numbers followed by a hyphen followed by two uppercase letters.

Pattern is followed by a regular expression. See other books for the syntax of regular expressions.

Correct: 123-AB error: abc-AB123-ab

Example 2 of simple types

This is a type USState that describes the names of states in the United States. All state names are listed through enumeration, and only the state names listed inside can be taken for values.

Tags: Elements types attributes content documents addresses data schemas diagrams instances this is at the same time models errors sizes information methods comments structures parts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Apple macOS OPPO Reno