Three ways to write XML (3)--schema files

Source: Internet
Author: User
How to write a schema document
①russian Doll (Russian set of dolls)
②salami Slice (sausage slices)
③venetian Blind (blinds) Recommended

Second, Russian doll Russia set Eva

As the name suggests, the way to write is a layer, only a root element, through and set the way to write complete.
Advantages: Clear Structure
Disadvantage: elements cannot be reused

Russiondoll.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.xy.com"
Xmlns:tns= "Http://www.xy.com"
elementformdefault= "qualified" >

<element name= "Books" >
<complexType>
<!--maxoccurs represents the maximum number of occurrences, unbounded the number of times unlimited. The default number is 1 times-->
<sequence maxoccurs= "unbounded" >
<element name= "book" >
<complexType>
<sequence minoccurs= "1" maxoccurs= "unbounded" >
<element name= "title" Type= "string"/>
<element name= "Content" type= "string"/>
<!--choice label attributes optional-->
<choice>
<element name= "Author" type= "string"/>
<element name= "Authors" >
<complexType>
<!--the elements in the All tab can appear sequentially, but the number is 1 times-->
<all>
<!--only one--> per element appears
<element name= "Author" type= "string"/>
</all>
</complexType>
</element>
</choice>
</sequence>
<!--the properties of the book, which must be--> after sequence in the schema
<attribute name= "id" type= "int" use= "required"/>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>


Russiondoll.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<book:books xmlns:book= "http://www.example.org/02"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "/02.xsd" >
<book:book id= "1" >
<book:title>java in Action</book:title>
<book:content>java is good</book:content>
<book:author>TOM</book:author>
</book:book>
<book:book id= "2" >
<book:title>soa in Action</book:title>
<book:content>soa is difficult</book:content>
<book:authors>
<book:author>LILY</book:author>
</book:authors>
</book:book>
</book:books>


second, salami slice sausage slices
Benefits: can be maximized for reuse
Disadvantage: The root element is not clear. Book, ID, title, content all of the element can be the root element
Salamislice.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.xy,com"
Xmlns:tns= "Http://www.xy.com"
elementformdefault= "qualified" >

<element name= "book" type= "Tns:booktype" ></element>
<element name= "id" type= "int"/>
<element name= "title" Type= "string"/>
<element name= "Content" type= "string"/>

<complextype name= "BookType" >
<sequence>
<element ref= "Tns:id"/>
<element ref= "Tns:title"/>
<element ref= "Tns:content"/>
</sequence>
</complexType>
</schema>


third, shutters venetian blind
root element clear, element reusable
Venetianblind.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.xy.com"
Xmlns:tns= "Http://www.xy.com"
elementformdefault= "qualified" >

<element name= "Person" type= "Tns:persontype"/>

<complextype name= "PersonType" >
<sequence>
<element name= "Name" type= "string"/>
<element name= "Age" type= "Tns:agetype"/>
<element name= "Email" type= "Tns:emailtype"/>
</sequence>
<attribute name= "Sex" type= "Tns:sextype"/>
</complexType>

<simpletype name= "Emailtype" >
<restriction base= "string" >
<pattern value= "(\w+\.*) *\w+@\w+\. [A-za-z] {2,6} "/>
<minlength value= "6"/>
<maxlength value= "255"/>
</restriction>
</simpleType>

<simpletype name= "Agetype" >
<restriction base= "int" >
<mininclusive value= "1"/>
<maxexclusive value= "/>"
</restriction>
</simpleType>

<simpletype name= "Sextype" >
<restriction base= "string" >
<enumeration value= "Male"/>
<enumeration value= "female"/>
</restriction>
</simpleType>
</schema>

Venetianblind.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<person xmlns= "http://www.example.org/04"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://www.xy.com" sex= "male" >
<name>test</name>
<age>10</age>
<email>123456@QQ.com</email>
</person>


A complex DTD
Venetianblind2.xsd
<?xml version= "1.0" encoding= "UTF-8"?>
<schema xmlns= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "Http://www.xy.com"
Xmlns:xy= "Http://www.xy.com"
elementformdefault= "qualified" >

<element name= "Persons" type= "Xy:personstype"/>

<complextype name= "Personstype" >
<sequence maxoccurs= "unbounded" >
<element name= "Person" type= "Xy:persontype" ></element>
</sequence>
</complexType>

<complextype name= "PersonType" >
<sequence>
<element name= "Name" type= "string"/>
<element name= "Age" type= "Xy:agetype"/>
<element name= "Email" type= "Xy:emailtype"/>
</sequence>
<attribute name= "Sex" type= "Xy:sextype" default= "male"/>
<attribute name= "id" type= "id" use= "required"/>
</complexType>

<simpletype name= "Emailtype" >
<restriction base= "string" >
<pattern value= "(\w+\.*) *\w+@\w+\. [A-za-z] {2,6} "/>
<minlength value= "6"/>
<maxlength value= "255"/>
</restriction>
</simpleType>

<simpletype name= "Agetype" >
<restriction base= "int" >
<mininclusive value= "1"/>
<maxexclusive value= "/>"
</restriction>
</simpleType>

<simpletype name= "Sextype" >
<restriction base= "string" >
<enumeration value= "Male"/>
<enumeration value= "female"/>
</restriction>
</simpleType>
</schema>

Venetianblind2.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<persons xmlns= "Http://www.xy.com"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.xy.com" >

<person sex= "female" id= "P1" >
<name>test</name>
<age>10</age>
<email>123456@qq.com</email>
</person>
<person sex= "Male" id= "P2" >
<name>test2</name>
<age>20</age>
<email>123456@qq.com</email>
</person>
</persons>

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.