The foundation of the schema:
<?xml version= "1.0" encoding= "UTF-8"?> <!--The advantages of the schema: The purpose of 1.schema is to write XML restriction files in a more logical way (based on XML syntax) 2. Schema can use namespaces to support multiple names with the same element 3.schema can do a good job of decorating Java or objects and providing a large number of data types schema elements: Default namespace (no colon): xmlns= "/http" Www.w3.org/2001/XMLSchema "targetnamespace: Defines the namespace of the current schema file (other values that need to be introduced when introduced) or other elements of the current file that are introduced in the current schema TNS: before the specified namespace Prefix named TNS example: <element name= "tt" type= "tns:test" ></element> <complextype name= "test" ></complextype > <schema xmlns= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "Http://www.example.org/user" Targetnamespace= "Http://www.example.org/user" elementformdefault= "qualified" > <element name= "User" > <! --Complex Type-<complexType> <sequence> <element name= "id" type= "int"/> <element name= "u Sername "type=" string "/> <element name=" Born "type=" date "/> </sequence> <!--sequence--</
Complextype> </element> </schema>
There are two ways to generate User.xml files based on user.xsd:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Eclipse Auto Tip:
window-preferrece-xml-xml Catalog and then click Add--
<user xmlns= "Http://www.example.org/user"
xmlns:xsi= "http://www.w3.org/ 2001/xmlschema-instance "
xsi:schemalocation=" Http://www.example.org/user ">
<id>1</id>
<username>zhangsan</username>
<born>1988-11-12</born>
</user>
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Eclipse Auto Tip:
window-preferrece-xml-xml Catalog and then click Add
xsi:nonamespaceschemalocation: Direct access to the XSD file--
<user xmlns= "/http Www.example.org/user "
xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "
xsi: nonamespaceschemalocation= "User.xsd" >
<id>1</id>
<username>zhangsan</ username>
<born>1988-11-12</born>
</user>
Three commonly used schema writing formats:
The first type: Russian Doll way:
<?xml version= "1.0" encoding= "UTF-8"?> <!--Russian doll way: clear structure; but elements cannot be reused--<schema xmlns= "http://www.w3.org /2001/xmlschema "xmlns:tns=" Http://www.example.org/books "targetnamespace=" Http://www.example.org/books "
elementformdefault= "qualified" > <!--Books is a responsible type and then three properties: Title, content, and author author and select attributes: Author or authors Authors is a responsible attribute: contains three kinds of author complextype: sequence, choice, all sequence: The elements must appear in order choice: Select one all in the multiple: so the same element can only be Define the properties of the responsible type once]: (defined after sequence) attribute--<element name= "Books" > <complexType> <seque nce minoccurs= "1" maxoccurs= "unbounded" > <element name= "book" > <complexType> <sequence mi noccurs= "1" maxoccurs= "unbounded" > <element name= "title" Type= "string"/> <element name= "Content" Type= "string"/> <choice> <element name= "Author" type= "string"/> <element name= "a
Uthors "> <complexType> <sequence minoccurs= "1" maxoccurs= "unbounded" > <element name= "Author" type= "string"/> </sequence> </complexType> </element> </choice> </sequence&
Gt
<attribute name= "id" type= "int" use= "required"/> </complexType> </element> </sequence> <!--minoccurs= "1" set at least once, maxoccurs= "unbounded" up to No limit; By default one time--</complexType> </element> </sch
Ema>
The first format generates the file:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Eclipse Auto Tip:
window-preferrece-xml-xml Catalog then click Add--
<books:books xmlns:books= "http://www.example.org/books"
xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" Http://www.example.org/books ">
<!-- Elements are also ordered--
<books:book id= "1" >
<books:title>java in action</books:title>
< Books:content>java is good</books:content>
<books:author>Bruce</books:author>
</books:book>
<books:book id= "2" >
<books:title>lucence in actioin</books:title>
<books:content>lucence is good</books:content>
<books:authors>
<books: author>jike</books:author>
<books:author>Mike</books:author>
</books:authors >
</books:book>
</books:books>
The second type: sausage-style way:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Sausage Slicing method:
structure not clear--
<schema xmlns= " Http://www.w3.org/2001/XMLSchema "xmlns:tns=" http://www.example.org/book02 "targetnamespace="/HTTP/ Www.example.org/book02 "elementformdefault=" qualified ">
<element name=" book "type=" Tns:booktype "/>
<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>
The second type of XML:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Eclipse Auto Tip:
window-preferrece-xml-xml Catalog then click Add--
<book
xmlns:book= "http://www.example.org/book02"
xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://www.example.org/book02 ">
<id> </id>
<title></title>
<content></content>
</book>
Third-party shutter type:
<?xml version= "1.0" encoding= "UTF-8"?> <schema xmlns= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "http// Www.example.org/person "targetnamespace=" Http://www.example.org/person "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:ema Iltype "/> </sequence> <attribute name=" Sex "type=" Tns:sextype "/> </complexType> <simpletype Name= "Agetype" > <restriction base= "int" > <mininclusive value= "1"/> <maxexclusive value= "/>"
; <!--minimum includes 1--<!--Max does not include--</restriction> </simpleType> <simpletype name= "Sextype" &
Gt <restriction base= "string" > <enumeration value= "male"/> <enumeration value= "female"/> </RESTRICTION&G
T </simpleType> <sImpletype name= "Emailtype" > <restriction base= "string" > <minlength value= "6"/> <maxlength value=
"255"/> <pattern value= "(\w+\.*) *\w+@\w+"/> </restriction> </simpleType> </schema>
The third example of XML generation:
<?xml version= "1.0" encoding= "UTF-8"?>
<!--
Eclipse Auto Tip:
window-preferrece-xml-xml Catalog and then click Add--
<person:person
xmlns:person= "Http://www.example.org/person"
xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.example.org/person" sex= "male" >
<person:name>aa</person:name>
<person:age>12</person:age>
<person: Email>sdf@sdf</person:email>
</person:person>
The final comprehensive test example:
<?xml version= "1.0" encoding= "UTF-8"?> <schema xmlns= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "Http://www.example.org/classroom" targetnamespace= "Http://www.example.org/classroom" elementformdefault= "qualified" > <element name= "Student" type= "Tns:studenttype"/> <complextype name= " Studenttype "> <sequence> <element name=" name "type=" string "/> <element name=" age "type=" Tns:aget Ype "/> </sequence> <attribute name=" Sex "type=" Tns:sextype "/> </complexType> <simpletype Nam
E= "Agetype" > <restriction base= "int" > <mininclusive value= "1"/> <maxexclusive value= "/>" <!--minimum includes 1--<!--Max does not include--</restriction> </simpleType> <simpletype name= "Sextype "> <restriction base=" string "> <enumeration value=" male "/> <enumeration value=" female "/> </restr iction> </simpleType> </schema>
<?xml version= "1.0" encoding= "UTF-8"?> <!--schema convert Java file xjc-d d:/java-verbose classroom.xsd-<SC Hema xmlns= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "http://www.example.org/classroom" targetnamespace= "http ://www.example.org/classroom "elementformdefault=" qualified "> <!--if you want to be inductive and call each other, the namespace will be the same--< Include schemalocation= "student.xsd"/> <element name= "classroom" type= "Tns:classroomtype"/> < ComplexType name= "Classroomtype" > <sequence> <element name= "Grade" type= "Tns:gradetype"/> <eleme NT Name= "name" type= "string"/> <!--<element name= "Stus" > <complexType> <sequence min
occurs= "1" maxoccurs= "unbounded" > <element name= "Student" type= "Tns:studenttype"/> </sequence> </complexType> </element> <sequence minoccurs= "1" maxoccurs= "unbounded" > <ele ment name= "Student" type= "Tns:studenttype"/> </sequence> </sequence> </complexType> <simpletype name= "Gradetype" > <restriction base= "int" >
<mininclusive value= "1"/> <maxexclusive value= "min"/> <!--minimum includes 1--<!--Max not included
</restriction> </simpleType> </schema>