XML Schema Tutorial

xiaoxiao2021-03-06  25

1. The simplest Schema document

How to write a simplest XML Schema document?

First, we write a simplest XML document.

Hello.xml

-------------------

Hello World !!

Hello.xsd

------------

XML Schema document After the suffix name is .xsd, fully compliant xml syntax, root element is schema, named space XMLns: XSD = "http://www.w3.org/2001/xmlschema, with element defined in instance documentation Elements such as Greeting.

2. SCHEMA document of anointed child

Suppose the instance document is as follows:

Customer.xml

-----------

teiki

No.237, Road Waitan,

Shanghai

The following XML Schema documentation can be written:

Customer.xsd

----------------

In the instance document Customer.xml, the element contains two sub-elements, so we use complexType in the Schema document to define this element. The sequence represents the order in which the sub-elements appear.

3. Schema documentation with child elements and grandson

This time we give a more complex document:

Customer.xml

---------------

teiki

Zhejiang

Hangzhou

Xilu Road

, No.121,

7f

To this end, we need a more complicated Schema documentation:

Address.xsd

-----------------

However, we can also use the REF element to rewrite this Schema document:

Address2.xsd

----------------------

Use the REF element to point directly to another module to make the document more readable.

4. Define the amount of the same subtype

First look at this simple order data instance document:

ORDER.XML

---------

Accounting Book

Taxation Book

Suppose elements, that is, each time the order book cannot exceed 10 kinds, how do you write this Schema document? Here you need to use the maxoccurs attribute of .

ORDER.XSD

--------------------

The maxoccurs attribute in line 7 is 10, which represents the ORDERITEM element can have a maximum of 10. If, do not set the number of elements, you can use maxoccurs = "unbounded" to define.

Similarly, if you want to define a minimum, you can use Minoccurs, such as the following:

These two attribute defaults are 1.

5. Define the child elements of options

If the booking data above, you can order any one of the book name or book number, the instance document may be as follows:

ORDER2.XML

-----------------

Accounting book

7-5058-3496-7

At this time, you should write the SCHEMA document and you need to use the Choice element.

ORDER2.XSD

-------------------------

Slightly more complex optional items

Slightly modify the instance documentation of the booking data:

ORDER3.XML

-----------------

Accounting book

2

7-5058-3496-7

It is defined when the value is 1, is default.

How to modify the Schema documentation?

ORDER3.XSD

-----------------

There are fewer values ​​in the 19 lines of QUANTITY to 0, that is, there can be, no. Of course, you can also directly in the element, contain quantity, and then define it's MinoCCURS.

6. Built-in simple type

省 省 省 省

7. Customized Simple Type

What should I do if the 44 types of the built-in simple type cannot meet the requirements? Learn the custom simple type below. (XML scalability is fully reflected here)

For example, this example document:

ORDER4.XML

-----------------

7-5058-3496-7

5

ID is a standard ISBN code, how do we define this ISBN coding?

iDType is a custom simple type.

We have made it for it:

Represents it is based on a string type. The form of the string is described again with the Pattern element.

Value = "/ d {1} - / d {4} - / d {4} - / d {1}" This is a regular expression, about the regular expression, later introduces. Hey!

Using this customized simple type, we can rewrite the Schema documentation:

ORDER4.XSD

---------------

If we have prior to determine how only 3 is only 3, that is, only 3 ISBN is optional, what should I do? We can use Enumeration elements to list.

Let's take a look at the value of the order quantity, if we set its value between 1-10, what should I do? You can customize a simple type.

Among them, MININCLUSIVE, MAXINCLUSIVE represents the value range of this type, respectively.

So the final revised Schema document is as follows:

ORDER4-1.xSD

----------------------

8. Define properties

Finally, let's talk about how the properties of the elements are defined in the Schema documentation.

For example, in the Order.xml instance document:

In this regard, we use an Attribute in the Schema document:

ORDER.XSD

---------

← empty element

So, is this attribute value in the instance document or no? We can limit this:

Here we told the id attribute type as a custom data type IDTYPE. Moreover, use the USE attribute of the Attribute element to define whether it is a must.

Required is a must value, Optional is an optional value, and Prohibited is no attribute value.

So how do we define for the default values ​​of attributes? such as:

We can also define it with another attribute of Attribute elements. DEFAULT:

So, we can rewrite a Schema document:

ORDER2.XSD

----------------

The above attributes We define that we can also use the property group to rewrote the Schema document.

ORDER3.XSD

----------------

This attribute group will not explain in detail, but everyone will clear it.

Finally, we write a complete booking Order.xml Schema document.

Quote XSD in other XML

The corresponding corresponds to:

ElementFormDefault = "Qualified" means that the Namespace used by Element is TargetNameSpace, which is used to use "limited and non-qualified" to the element. It means that the namespace prefix is ​​required in the document example.

转载请注明原文地址:https://www.9cbs.com/read-65441.html

New Post(0)