Learn XML Schema with me (transfer)

xiaoxiao2021-03-06  55

To learn XML Schema (1): How do the simplest Schema document written a simplex XML Schema document?

First, we write a simplest XML document.

Hello.xml ------------------ Hello World !!

(A root element: greeting; and this element does not contain attributes, no child elements, the content is a string.)

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 Define the instance document Elements, such as Greeting. Tong XML Schema (2): Slightly Complex SCHEMA Document with Child Elements

Suppose the instance document is as follows:

Customer.xml ----------- teiki

No.237, Road Waitan, Shanghai

You can write the following XML Schema document: Customer.xsd ---------------- 1: 2: 3: 4: 5: 6: 7: 8: 9: 10 : 11:

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. Learn XML Schema (3): More Complex Schema Documents with Chubs and Sun Yuan

This time we give a more complex document:

Address.xml --------------- teiki

zhejiang < / prefect> Hangzhou Xilu Road, No.121, 7F To this end, we need a more complicated Schema document:

Address.xsd ----------------- 1: 2: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: < / xsd: completion> 21: 22:23: However, we can also use the REF element to rewrite this Schema document: address2.xsd --------- ------------- 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11 : 12:13: 14: 15: 16:

Type = "xsd: string" /> 17: 18: 19 : 20: 21: 22:23: Using the REF element can directly point to another module, making the document more Readability. Learn XML Schema (4): How to define attributes? Elements to join the instance document contain attributes, what should I do?

Customer2.xml --------------- teiki

No.237, Road Waitan, Shanghai < / Customer>

That is like this to write Schema document: Customer2.xsd ------------------ 1: 2: 3:4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:15:

Very simple, after the element is defined, use the element ID. Learn XML Schema (5): How to define the number of identivers of the same child?

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 -------------------- 1: 2: >:4: 5: 6: 7: 8: 9: 10: 11:12: section 7 The maxoccurs attribute in the row is 10, and the representative ORDERITEM element can be up to 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 attributes Values ​​are 1. Learn XML Schema (6): How to define the sub-elements of optional 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 < OrderItem> 7-5058-3496-7

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

Order2.xsd ------------------------- 1: 2: 3:4: 5: 6: 7: 8: 9: 11:12: 13: 15: 16: 17: 18: 19: 20:21: learning XML Schema (7): Slightly more complex Optional item elements are slightly modified instance documents 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 ---------------- 1: 2: 5: 6: 7: 8: 9: 10: 11:12: 13: 14 : 16: 17: 18: 19: 20: 21: 22: 23:24: 19 rows of Quantity at least 0, that is, there can be, or no. Of course, you can also directly in the element, contain quantity, and then define it's MinoCCURS. Learn XML Schema (8): Built-in simple type

There are 44 simple types built in XML Schema. They are published in the second part of the XML Schema recommended standard, which is a hierarchical diagram of a built-in type:

http://www.w3.org/tr/2001/pr-xmlschema-2-20010330/Type-hierarchy.jpg Tong Xun XML Schema (9): 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 make a restrictions on 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 --------------- 1: 2: 5: 7: 8: 10: 11:12: 13: 14: < XSD: sequence> 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: < / xsd: schema>

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.

to see the value of the order quantity, if we set its value What should I do between 1-10? 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 ---------------------- 1: 2: 3:4: 5: 6: 7: 8: 9: 11:12: 13 : 15: 16: 17 : 18: 19: 20: 21: 22: 23: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:36: XML Schema (10): Define attributes Finally, let's talk about How the element's properties are defined in the Schema document.

For example, in the Order.xml instance document:

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

Order.xsd --------- ← Empty element So, this property value is still unable to ? 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? For example:

We can also define it with another attribute of attribute element 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 ---------------- 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: This attribute group will not explain in detail, but everyone will clear it.

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

New Post(0)