1. The simplest Schema document
How to write a simplest XML Schema document?
First, we write a simplest XML document.
Hello.xml
-------------------
XML Version = "1.0"?>
Hello.xsd
------------
XML Version = "1.0"?>
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
2. SCHEMA document of anointed child
Suppose the instance document is as follows:
Customer.xml
-----------
Shanghai
address>
Customer>
The following XML Schema documentation can be written:
Customer.xsd
----------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: schema>
In the instance document Customer.xml, the
3. Schema documentation with child elements and grandson
This time we give a more complex document:
Customer.xml
---------------
Zhejiang
prefecture>
Hangzhou
city>
Xilu Road
, No.121,
7f
street>
address>
Customer>
To this end, we need a more complicated Schema documentation:
Address.xsd
-----------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: schema>
However, we can also use the REF element to rewrite this Schema document:
Address2.xsd
----------------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: schema>
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
---------
order>
Suppose
ORDER.XSD
--------------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: schema>
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
-----------------
OrderItem>
order>
At this time, you should write the SCHEMA document and you need to use the Choice element.
ORDER2.XSD
-------------------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: choice>
xsd: complexType>
xsd: element>
xsd: schema>
Slightly more complex optional items
Slightly modify the instance documentation of the booking data:
ORDER3.XML
-----------------
OrderItem>
OrderItem>
order>
It is defined when the
How to modify the Schema documentation?
ORDER3.XSD
-----------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: choice>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: schema>
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
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
-----------------
OrderItem>
order>
ID is a standard ISBN code, how do we define this ISBN coding?
xsd: restriction>
xsd: SimpleType>
iDType is a custom simple type.
We have made it for it:
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
---------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: restriction>
xsd: SimpleType>
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.
xsd: restriction>
xsd: SimpleType>
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.
xsd: restriction>
xsd: SimpleType>
Among them, MININCLUSIVE, MAXINCLUSIVE represents the value range of this type, respectively.
So the final revised Schema document is as follows:
ORDER4-1.xSD
----------------------
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: restriction>
xsd: SimpleType>
xsd: restriction>
xsd: SimpleType>
xsd: schema>
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:
order>
In this regard, we use an Attribute in the Schema document:
ORDER.XSD
---------
xsd: sequence>
xsd: complexType>
xsd: 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:
order>
We can also define it with another attribute of Attribute elements. DEFAULT:
So, we can rewrite a Schema document:
ORDER2.XSD
----------------
xsd: complexType>
xsd: element>
The above attributes We define that we can also use the property group to rewrote the Schema document.
ORDER3.XSD
----------------
xsd: complexType>
xsd: element>
xsd: attributegroup>
This attribute group will not explain in detail, but everyone will clear it.
Finally, we write a complete booking Order.xml Schema document.
XML Version = "1.0"?>
xsd: sequence>
xsd: complexType>
xsd: element>
xsd: complexType>
xsd: element>
xsd: attributegroup>
xsd: restriction>
xsd: SimpleType>
xsd: schema>
Quote XSD in other XML
XML Version = "1.0" encoding = "UTF-8" Standalone = "no"?>
The corresponding corresponds to:
XML Version = "1.0"?>
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.