webservice(三)有关schema的基础知识

来源:互联网 发布:js中switch case语句 编辑:程序博客网 时间:2024/05/22 07:57

schema的基础:

<?xml version="1.0" encoding="UTF-8"?><!--schema的优点: 1.schema出现的目的是通过一个更加合理的方式来编写xml限制文件(基于xml语法的方式) 2.schema可以使用命名空间来支持多个名称相同的元素 3.schema可以很好的完成对java或者所以对象的修饰并且提供了大量的数据类型schema元素:默认的命名空间(没有冒号):xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace:定义当前schema文件的命名空间(其他引入的时候需要引入的值)也可以在当前schema中引入当前文件的其他元素tns:指定的命名空间前缀名为tns例子:<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"><!-- 复杂类型 --><complexType><sequence><element name="id" type="int"/><element name="username" type="string"/><element name="born" type="date"/></sequence><!-- 序列 --></complexType></element></schema>
根据user.xsd生成user.xml文件两种引入的方式:

<?xml version="1.0" encoding="UTF-8"?><!-- eclipse自动提示:window-preferrece-xml-xml catalog然后点击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自动提示:window-preferrece-xml-xml catalog然后点击addxsi:noNamespaceSchemaLocation:直接访问xsd文件 --><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>

三种常用的schema编写格式:

第一种:俄罗斯玩偶方式:

<?xml version="1.0" encoding="UTF-8"?><!-- 俄罗斯玩偶方式:结构清晰;但是元素无法重用 --><schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/books" targetNamespace="http://www.example.org/books" elementFormDefault="qualified"><!-- books是一个负责类型 然后又三个属性:title、content和author author又是选择属性:选author或者authors authors是个负责属性:包含author complexType中三种:sequence、choice、allsequence:元素必须按顺序出现choice:多者中选择一个all:所以相同的元素只能出现一次]定义负责类型的属性:(在sequence之后进行定义)attribute--><element name="books"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="book"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="title" type="string"/><element name="content" type="string"/><choice><element name="author" type="string"/><element name="authors"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="author" type="string"/></sequence></complexType></element></choice></sequence><attribute name="id" type="int" use="required"/></complexType></element></sequence><!-- minOccurs="1"设置最少一次, maxOccurs="unbounded"最多不限制;默认是一次 --></complexType></element></schema>

第一种格式生成文件:

<?xml version="1.0" encoding="UTF-8"?><!-- eclipse自动提示:window-preferrece-xml-xml catalog然后点击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"><!-- 元素也是有顺序的 --><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>

第二种:腊肠式方式:

<?xml version="1.0" encoding="UTF-8"?><!-- 香肠切片方式:结构不清晰 --><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>

第二种xml方式:

<?xml version="1.0" encoding="UTF-8"?><!-- eclipse自动提示:window-preferrece-xml-xml catalog然后点击add --><bookxmlns: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>

第三者百叶窗式的:

<?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:emailType"/></sequence><attribute name="sex" type="tns:sexType"/></complexType><simpleType name="ageType"><restriction base="int"><minInclusive value="1"/><maxExclusive value="150"/><!-- 最小值包含1 --><!-- 最大值不包含 --></restriction></simpleType><simpleType name="sexType"><restriction base="string"><enumeration value="男"/><enumeration value="女"/></restriction></simpleType><simpleType name="emailType"><restriction base="string"><minLength value="6"/><maxLength value="255"/><pattern value="(\w+\.*)*\w+@\w+"/></restriction></simpleType></schema>

第三种的xml生成实例:

<?xml version="1.0" encoding="UTF-8"?><!-- eclipse自动提示:window-preferrece-xml-xml catalog然后点击add --><person:personxmlns:person="http://www.example.org/person"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.example.org/person" sex="男"><person:name>aa</person:name><person:age>12</person:age><person:email>sdf@sdf</person:email></person:person>

最后的综合测试实例:

<?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:ageType"/></sequence><attribute name="sex" type="tns:sexType"/></complexType><simpleType name="ageType"><restriction base="int"><minInclusive value="1"/><maxExclusive value="150"/><!-- 最小值包含1 --><!-- 最大值不包含 --></restriction></simpleType><simpleType name="sexType"><restriction base="string"><enumeration value="男"/><enumeration value="女"/></restriction></simpleType></schema>

<?xml version="1.0" encoding="UTF-8"?><!--schema转换java文件xjc -d d:/java -verbose classroom.xsd --><schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/classroom" targetNamespace="http://www.example.org/classroom" elementFormDefault="qualified"><!-- 如果想引人,并且互相调用的话,命名空间要为同样的 --><include schemaLocation="student.xsd"/><element name="classroom" type="tns:classroomType"/><complexType name="classroomType"><sequence><element name="grade" type="tns:gradeType"/><element name="name" type="string"/><!-- <element name="stus"><complexType><sequence minOccurs="1" maxOccurs="unbounded"><element name="student" type="tns:studentType"/></sequence></complexType></element> --><sequence minOccurs="1" maxOccurs="unbounded"><element name="student" type="tns:studentType"/></sequence></sequence></complexType><simpleType name="gradeType"><restriction base="int"><minInclusive value="1"/><maxExclusive value="150"/><!-- 最小值包含1 --><!-- 最大值不包含 --></restriction></simpleType></schema>


原创粉丝点击