Webservice(基础)——schema
上篇博客我们讲到了DTD,虽然其能够实现对xml文件的限定,但是由于其标签内容不足,限定的不尽“完美”,所以有了Schema。 一、基本含义:可以参考上篇DTD,都是对xml的一种限定和规范! 二、实例驱动:实例1、classroom.xsd
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/ClassRoom" xmlns:ClassRoom="http://www.example.org/ClassRoom" elementFormDefault="qualified"> <element name="ClassRoom"> <complexType> <sequence> <element name="Id" type="int" /> <element name="grade" type="string"/> <element name="name" type="string"/> <element name="students"> <complexType> <sequence minOccurs="1" maxOccurs="unbounded"> <element name="student"> <complexType> <sequence> <element name="stuname" type="string" /> <element name="age" type="int" /> </sequence> <attribute name="id" type="int" use="required"/> </complexType> </element> </sequence> </complexType> </element> </sequence> </complexType> </element> </schema> ? ? 简单理解: ? ? 只有一个根元素ClassRoom,下面包括Id(int类型),grade(string类型),name(string类型)和students(复合类型)四类标签;而students则包含student(复合类型)元素,且student最少出现一次,最多出现次数不限制,而且student具有id属性(int类型且必须),stuname元素(string类型),age类型(int类型) ? ? 对应的xml文件
<?xml version="1.0" encoding="UTF-8"?> <ClassRoom xmlns="http://www.example.org/ClassRoom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/ClassRoom"> <Id>123</Id> <grade>2010级</grade> <name>历史学</name> <students> <student id="123"> <stuname>肥磊</stuname> <age>23</age> </student> <student id="234"> <stuname>肥波</stuname> <age>23</age> </student> <student id="34"> <stuname>肥罗</stuname> <age>23</age> </student> </students> </ClassRoom> ps:以上这种schema编写方式叫俄罗斯套娃娃,一个跟元素,其它子元素都在不断往里层叠,类似于俄罗斯套娃娃! 实例2、person3.xsd
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/person3" xmlns:person3="http://www.example.org/person3" elementFormDefault="qualified"> <element name="person" type="person3:persontype"/> <complexType name="persontype"> <sequence> <element name="name" type="string"/> <element name="age" type="person3:ageType"/> </sequence> <attribute name="sex" type="person3:sexType"/> </complexType> <simpleType name="ageType" > <restriction base="int"> <minInclusive value="1"/> <maxExclusive value="150"/> </restriction> </simpleType> <simpleType name="sexType"> <restriction base="string"> <enumeration value="男"/> <enumeration value="女"/> </restriction> </simpleType> </schema> 简单理解:同样只有一个person根元素,它的类型为persontype,而persontype又包含sex属性(枚举类型),name元素(string类型),age元素(ageType类型,最大150,最小为1)。 ps这种样式风格的schema编写风格被称作百叶窗编写方式 对应的xml文件
<?xml version="1.0" encoding="UTF-8"?> <person xmlns="http://www.example.org/person3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/person3" sex="男"> <name>磊磊</name> <age>149</age> </person> 个人认为Schema的关键点在与targetnamespace,理解了这个内容,其余的基本就好说了。 三、小结DTD和Schema为限制而生!基本上在很多主流的java开源框架都用到这两者,尤其是Schema,spring,hibernate,struts无一例外!而Webservice的关键内容wsdl亦是基于Schema的一种实现,因此将DTD和Schema两者作为Webservice回顾的一个基本知识来学习一下! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |