XML Summary (2) Schema

Source: Internet
Author: User

1. Simplest schema document
How to Write a simple XML schema document?

First, we will write out the simplest XML document.

Hello. xml

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

<? XML version = "1.0"?>

<Greeting> Hello world !! </Greeting>

<! -- A root element: greeting. This element does not contain any attribute and has no child element. The content is a string. -->

Hello. XSD

----------

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "greeting" type = "XSD: string"/>
</XSD: schema>

The extension name of the XML schema document is. XSD, which fully complies with the XML syntax. The root element is schema, And the namespace xmlns: XSD = "http://www.w3.org/2001/xmlschema. the element <element> is used to define the elements in the instance document,.

2. schema documents containing child elements
Assume that the instance document is as follows:

Customer. xml

-----------

<Customer>

<Name> teiki </Name>

<Address> No. 237, road Waitan, Shanghai </address>

</Customer>

You can write the following XML Schema documents:

Customer. XSD

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "customer">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "name" type = "XSD: string"/>

<XSD: element name = "Address" type = "XSD: string"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

In the instance document customer. XML, the <customer> element contains two child elements. Therefore, we use complextype In the schema document to define this element. Sequence indicates the sequence in which child elements appear in sequence.

3. schema documents containing child and Sun Elements
This time we provide a more complex document:

Customer. xml

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

<Customer>

<Name> teiki </Name>

<Address>

<! -- Address: append an address sub-element -->

<Prefecture> Zhejiang </prefecture>

<City> Hangzhou </city>

<Street> xilu Road, No. 121, 7f </street>

</Address>

</Customer>

To this end, we need a more complex schema document:

Address. XSD

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "customer">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "name" type = "XSD: string"/>

<! -- Append the sub-element address -->

<XSD: element name = "Address">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "Prefecture" type = "XSD: string"/>

<XSD: element name = "city" type = "XSD: string"/>

<XSD: element name = "street" type = "XSD: string"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

However, we can use the ref element to re-compile this schema document:

Address2.xsd

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "customer">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "name" type = "XSD: string"/>

<XSD: Element ref = "Address"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "Address">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "Prefecture" type = "XSD: string"/>

<XSD: element name = "city" type = "XSD: string"/>

<XSD: element name = "street" type = "XSD: string"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

Using the ref element, you can direct it to another module to make the document more readable.

4. Define the number of identical child elements
Let's take a look at this simple ordering data instance document:

Order. xml

---------

<Order>

<Orderitem> accounting book </orderitem>

<Orderitem> taxation book </orderitem>

</Order>

Assume that the number of <orderitem> elements cannot exceed 10, so how can we write this schema document? The maxoccurs attribute of <element> is used here.

Order. XSD

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "orderitem" type = "XSD: string" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

The maxoccurs attribute in row 7th is 10, which indicates that the orderitem element can have a maximum of 10. If you do not set the number of elements, you can use maxoccurs = "unbounded" to define them.

Similarly, if you want to define the minimum value, you can use minoccurs. For example:

<XSD: element name = "orderitem" type = "XSD: string" minoccurs = "5" maxoccurs = "10"/>

The default values of these two attributes are 1.

5. define child elements that are optional
If the book data above can be ordered by one of the book titles or book numbers, the instance document may be as follows:

Order2.xml

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

<Order>

<Orderitem>

<! -- Book title order -->

<Name> accounting book </Name>

</Orderitem>

<Orderitem>

<! -- Book number order -->

<ID> 7-5058-3496-7 </ID>

</Orderitem>

</Order>

In this case, you also need to use the choice element to write the schema document.

Order2.xsd

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: Element ref = "orderitem" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: Choice>

<XSD: element name = "name" type = "XSD: string"/>

<XSD: element name = "ID" type = "XSD: string"/>

</XSD: Choice>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

More complex option child elements

Modify the instance document of the book data slightly:

Order3.xml

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

<Order>

<Orderitem>

<Name> accounting book </Name>

<Quantity> 2 </quantity>

</Orderitem>

<Orderitem>

<ID> 7-5058-3496-7 </ID>

</Orderitem>

</Order>

It is assumed that the default value of <quantity> is 1.

How can I modify the schema document?

Order3.xsd

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: Element ref = "orderitem" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence>

<XSD: Choice>

<XSD: element name = "name" type = "XSD: string"/>

<XSD: element name = "ID" type = "XSD: string"/>

</XSD: Choice>

<XSD: element name = "quantity" type = "XSD: string" minoccurs = "0"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

</XSD: schema>

The quantity value in the 19 rows is at least 0, that is, either or not.
Of course, you can also include quantity in the <choice> element and define its minoccurs.

6. built-in simple type
Figure omitted

7. Custom simple type
What if 44 simple built-in types cannot meet the requirements? The following describes how to customize simple types. (XML scalability is fully reflected here)

For example, this instance document:

Order4.xml

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

<Order>

<Orderitem>

<ID> 7-5058-3496-7 </ID>

<Quantity> 5 </quantity>

</Orderitem>

</Order>

ID is a standard ISBN encoding. How do we define this ISBN encoding?

<XSD: simpletype name = "idtype">

<XSD: Restriction base = "XSD: string">

<XSD: Pattern value = "/d {1}-/d {4}-/d {4}-/d {1}"/>

</XSD: Restriction>

</XSD: simpletype>

Idtype is a simple custom type.

We have made restrictions on it:

<XSD: Restriction base = "XSD: string"> indicates that it is based on a string type. Use the pattern element to describe the string format.

Value = "/d {1}-/d {4}-/d {4}-/d {1}" is a regular expression. We will introduce it later. Hey!

With this simple custom type, we can re-write the schema document:

Order4.xsd

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: Element ref = "orderitem" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "ID" type = "idtype"/>

<XSD: element name = "quantity" type = "XSD: integer"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: simpletype name = "idtype">

<XSD: Restriction base = "XSD: string">

<XSD: Pattern value = "/d {1}-/d {4}-/d {4}-/d {1}"/>

</XSD: Restriction>

</XSD: simpletype>

</XSD: schema>

If we determine that there are only three IDs, that is, only three ISBN are optional, what should we do? We can use the enumeration element for listing.

<XSD: simpletype name = "idtype">

<XSD: Restriction base = "XSD: string">

<XSD: enumeration value = "7-5058-3496-7"/>

<XSD: enumeration value = "7-5005-6450-3"/>

<XSD: enumeration value = "7-3020-6069-7"/>

</XSD: Restriction>

</XSD: simpletype>

Let's take a look at the value of the Order Quantity quantity. What if we set the value to be between 1 and 10? You can customize a simple type.

<XSD: simpletype name = "quantitytype">

<XSD: Restriction base = "XSD: integer">

<XSD: min‑sive value = "1"/>

<XSD: maxcompute sive value = "10"/>

</XSD: Restriction>

</XSD: simpletype>

Mininclusive and maxcompute sive represent the value range of this type.

Therefore, the modified schema document is as follows:

Order4-1.xsd

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

<? XML version = "1.0"?>

<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: Element ref = "orderitem" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence>

<XSD: element name = "ID" type = "idtype"/>

<XSD: element name = "quantity" type = "quantitytype"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: simpletype name = "idtype">

<XSD: Restriction base = "XSD: string">

<XSD: enumeration value = "7-5058-3496-7"/>

<XSD: enumeration value = "7-5005-6450-3"/>

<XSD: enumeration value = "7-3020-6069-7"/>

</XSD: Restriction>

</XSD: simpletype>

<XSD: simpletype name = "quantitytype">

<XSD: Restriction base = "XSD: integer">

<XSD: min‑sive value = "1"/>

<XSD: maxcompute sive value = "10"/>

</XSD: Restriction>

</XSD: simpletype>

</XSD: schema>

 

8. Define attributes
Finally, let's talk about how to define element attributes in the schema document.

For example, in the order. XML instance document above:

<Order>

<Orderitem id = "7-5058-3496-7"/>

</Order>

In this regard, we use an attribute defined in the schema document:

Order. XSD

---------

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence> Empty sequence element

</XSD: sequence>

<! -- Define the element Property -->

<XSD: attribute name = "ID" type = "XSD: string"/>

</XSD: complextype>

</XSD: Element>

Is this attribute value mandatory or optional in the instance document? We can restrict it as follows:

<XSD: attribute name = "ID" type = "idtype" use = "required"/>

Here we will talk about the ID attribute type as a custom data type idtype.

In addition, the use Attribute of the attribute element is used to determine whether the attribute is required.

Required is a required value, optional is an optional value, and prohibited is a non-attribute value.

How can we define the default attribute values?
For example:

<Order>

<Orderitem id = "4-8443-1780-6" quantity = "3"/>

</Order>

We can also use another attribute default of attribute to define it:

<XSD: attribute name = "quantity" type = "XSD: integer" default = "1"/>

Therefore, we can write a schema document again:

Order2.xsd

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

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence> </XSD: sequence>

<XSD: attribute name = "ID" type = "idtype" use = "required"/>

<XSD: attribute name = "quantity" type = "XSD: integer" default = "1"/>

</XSD: complextype>

</XSD: Element>

We can use the attribute group method to rewrite the schema document.

Order3.xsd

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

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence> </XSD: sequence>

<XSD: attributegroup ref = "orderitemattributes"/>

</XSD: complextype>

</XSD: Element>

<XSD: attributegroup name = "orderitemattributes">

<XSD: attribute name = "ID" type = "idtype" use = "required"/>

<XSD: attribute name = "quantity" type = "XSD: integer" default = "1"/>

</XSD: attributegroup>

This attribute group is not explained in detail, but you can see it clearly at a glance.

Finally, we will write a complete schema document for order. xml.

<? XML version = "1.0"?>
<XSD: schema xmlns: XSD = "http://www.w3.org/2001/XMLSchema">

<XSD: element name = "order">

<XSD: complextype>

<XSD: sequence>

<XSD: Element ref = "orderitem" maxoccurs = "10"/>

</XSD: sequence>

</XSD: complextype>

</XSD: Element>

<XSD: element name = "orderitem">

<XSD: complextype>

<XSD: sequence> </XSD: sequence>

<XSD: attributegroup ref = "orderitemattributes"/>

</XSD: complextype>

</XSD: Element>

<XSD: attributegroup name = "orderitemattributes">

<XSD: attribute name = "ID" type = "idtype" use = "required"/>

<XSD: attribute name = "quantity" type = "XSD: integer" default = "1"/>

</XSD: attributegroup>

<XSD: simpletype name = "idtype">

<XSD: Restriction base = "XSD: string">

<XSD: Pattern value = "/d {1}-/d {4}-/d {4}-/d {1}"/>

</XSD: Restriction>

</XSD: simpletype>

</XSD: schema>

Reference XSD in other XML

<? XML version = "1.0" encoding = "UTF-8" standalone = "no"?>

<Envlope xmlns = "gmip" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation = "gmip_appraise_rep.xsd">

XSD corresponds:

<? XML version = "1.0"?>

<XSD: schema targetnamespace = "gmip" xmlns: XSD: "http://www.w3.org/2001/XMLSchema" elementformdefault = "qualified" attributeformdefault = "qualified">

Elementformdefault = "qualified" indicates that the namespace used by element is targetnamespace, which is used to "restrict and unrestrict" elements, the namespace prefix is required in the document example.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.