WebService(2)-XML系列之Schema

矫情吗;* 2022-08-01 15:44 209阅读 0赞

源码下载:链接:http://pan.baidu.com/s/1o69nBzO 密码: bbw2

一.定义

Schema同样用于检测XML是否符合语法规则。

二.点评

相对DTD而言,有如下优点:

1.语法和Xml相同

2.数据类型很多

3.提供域名机制,就是Java中的包

三.Xml中引入Schema

两种方式:

1.通过“命名空间”来引入

xml_user_02path.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- xmlns:是schema的默认命名空间,不能修改,但是可以增加前缀,如果增加前缀以后,则意味着创建所有element元素都需要增加前缀 -->
  3. <!-- targetNamespace:自己这个文档的命名空间,可以方便其他xml或者schema文件引用 -->
  4. <!-- xmlns:tns:此文件和自己的命名空间的名称是一致的,但是增加了tns的前缀,此时如果要引用当前文件所创建的类型,需要加上tns的前缀 -->
  5. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  6. targetNamespace="http://www.example.org/schema_user"
  7. xmlns:tns="http://www.example.org/schema_user"
  8. elementFormDefault="qualified">
  9. <!-- complexType创建负责类型 -->
  10. <element name="user">
  11. <complexType>
  12. <!--sequence有顺序的 -->
  13. <sequence>
  14. <element name="id" type="int"></element>
  15. <element name="username" type="string"></element>
  16. <element name="borndate" type="date"></element>
  17. </sequence>
  18. </complexType>
  19. </element>
  20. </schema>

xml_user_01namespace.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 第一种方式:xml中引用schema,通过:命名空间的方式 -->
  3. <!-- xmlns:xsi创建一个可以引用其它schema文件的命名空间 -->
  4. <!-- xsi:schemaLocation 引入其它【schema_user】的命名空间-->
  5. <user xmlns="http://www.example.org/schema_user"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.example.org/schema_user">
  8. <id>1</id>
  9. <username>赵栗婧</username>
  10. <borndate>2015-6-44</borndate>
  11. </user>

2.通过xsd的文档路径来引入

xml_user_02path.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- 第二种方式:xml中引用schema,通过:路径的方式 -->
  3. <!-- xsi:noNamespaceSchemaLocation 中指定了需要引用的文件【schema_user.xsd】 -->
  4. <user xmlns="http://www.example.org/schema_user"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:noNamespaceSchemaLocation="schema_user.xsd">
  7. <id>1</id>
  8. <username>赵栗婧</username>
  9. <borndate>2015-6-24</borndate>
  10. </user>

四.Schema的设计方式

3种:

1.RussianDoll

【特点】只有一个根元素,通过嵌套的方式完成编写

【优点】结构清晰,根元素只有一个

【缺点】元素无法重用
schema_1books_RussianDoll.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/schema_1books_RussianDoll"
  4. xmlns:tns="http://www.example.org/schema_1books_RussianDoll"
  5. elementFormDefault="qualified">
  6. <element name="books">
  7. <complexType>
  8. <!-- unbounded最大是没有限制的 -->
  9. <sequence maxOccurs="unbounded">
  10. <element name="book">
  11. <!-- 复杂类型 -->
  12. <complexType >
  13. <attribute name="id" type="int" use="required"></attribute>
  14. <!-- sequence设置是有序的;minOccurs最小是1;unbounded最大是没有限制的 -->
  15. <sequence minOccurs="1" maxOccurs="unbounded">
  16. <element name="bookid" type="int"></element>
  17. <element name="bookname" type="string"></element>
  18. <!-- 作者的人数是有选择的:1个或很多 -->
  19. <choice>
  20. <element name="auther" type="string"></element>
  21. <element name="authers" >
  22. <complexType>
  23. <!-- unbounded最大是没有限制的 -->
  24. <sequence maxOccurs="unbounded">
  25. <element name="auther" type="string"></element>
  26. </sequence>
  27. </complexType>
  28. </element>
  29. </choice>
  30. </sequence>
  31. </complexType>
  32. </element>
  33. </sequence>
  34. </complexType>
  35. </element>
  36. </schema>

xml_1books_RussianDoll.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <book:books xmlns:book="http://www.example.org/schema_1books_RussianDoll"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="schema_1books_RussianDoll.xsd">
  5. <book:book id="1">
  6. <book:bookid>1</book:bookid>
  7. <book:bookname>J2EE企业规范图书</book:bookname>
  8. <book:auther>赵栗婧</book:auther>
  9. </book:book>
  10. <book:book id="2">
  11. <book:bookid>2 </book:bookid>
  12. <book:bookname>J2EE企业规范图书</book:bookname>
  13. <book:authers>
  14. <book:auther>赵栗婧1</book:auther>
  15. <book:auther>赵栗婧2</book:auther>
  16. </book:authers>
  17. </book:book>
  18. </book:books>

2.Salami Slice

【特点】元素全部独立出来,后通过引用的方式进行引用

【优点】能够进行最大化重用

【缺点】根节点不清晰

schema_2books_SalamlSlice.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/schema_2books_SalamlSlice"
  4. xmlns:tns="http://www.example.org/schema_2books_SalamlSlice"
  5. elementFormDefault="qualified">
  6. <element name="book" type="tns:bookType"></element>
  7. <element name="id" type="int"></element>
  8. <element name="name" type="string"></element>
  9. <element name="content" type="string"></element>
  10. <complexType name="bookType">
  11. <sequence maxOccurs="unbounded">
  12. <element ref="tns:id"></element>
  13. <element ref="tns:name"></element>
  14. <element ref="tns:content"></element>
  15. </sequence>
  16. </complexType>
  17. </schema>

xml_2books_SalamlSlice.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <bookType xmlns="http://www.example.org/schema_2books_SalamlSlice"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="schema_2books_SalamlSlice.xsd">
  5. <id>1</id>
  6. <name>1</name>
  7. <content>1</content>
  8. </bookType>

3.CentianBind 推荐

【特点】只有一个根元素,但是通过s impleType完成引用

schema_3people_venetianbind.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/schema_3people_venetianbind"
  4. xmlns:tns="http://www.example.org/schema_3people_venetianbind"
  5. elementFormDefault="qualified">
  6. <element name="people" type="tns:peopleType"></element>
  7. <complexType name="peopleType">
  8. <sequence>
  9. <element name="name" type="string"/>
  10. <element name="age" type="tns:ageType"/>
  11. <element name="email" type="tns:emailType"/>
  12. </sequence>
  13. <attribute name="sex" type="tns:sexType"/>
  14. </complexType>
  15. <!-- 年龄的类型:是int类型的,并且在:1-150岁之间 -->
  16. <simpleType name="ageType">
  17. <restriction base="int">
  18. <minInclusive value="1"/>
  19. <maxExclusive value="150"/>
  20. </restriction>
  21. </simpleType>
  22. <!-- 性别的类型:男/女 -->
  23. <simpleType name="sexType">
  24. <restriction base="string">
  25. <enumeration value="男"/>
  26. <enumeration value="女"/>
  27. </restriction>
  28. </simpleType>
  29. <!-- 邮件类型的限制 -->
  30. <simpleType name="emailType">
  31. <restriction base="string">
  32. <!-- <pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z](2,6)"/> -->
  33. <minLength value="2"/>
  34. <maxLength value="255"/>
  35. </restriction>
  36. </simpleType>
  37. </schema>

xml_3people_venetianbind.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <people xmlns="http://www.example.org/schema_3people_venetianbind"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="schema_3people_venetianbind.xsd" sex="男">
  5. <name>赵栗婧</name>
  6. <age>20</age>
  7. <email>aaa</email>
  8. </people>

五..Schema之间的引用

两种:

1.非包装方式

2.包装方式

schema_classroom.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/schema_classroom"
  4. xmlns:tns="http://www.example.org/schema_classroom"
  5. elementFormDefault="qualified">
  6. <!-- 在classroom的schema中引入student的schema -->
  7. <xsd:include schemaLocation="schema_student.xsd"></xsd:include>
  8. <!-- 定义元素classroom,类型是classroomType -->
  9. <xsd:element name="classroom" type="tns:classroomType"></xsd:element>
  10. <!-- 定义复杂类型:classroomType -->
  11. <xsd:complexType name="classroomType">
  12. <xsd:sequence>
  13. <xsd:element name="name" type="xsd:string" />
  14. <xsd:element name="grade" type="tns:gradeType" />
  15. <!-- 开始:第一种:非包装的方式【建立classroom与student之间的关系】 -->
  16. <!-- <xsd:element name="stus"> -->
  17. <!-- <xsd:complexType> -->
  18. <!-- <xsd:sequence minOccurs="1" maxOccurs="100"> -->
  19. <!-- <xsd:element name="student" type="tns:studentType"></xsd:element> -->
  20. <!-- </xsd:sequence> -->
  21. <!-- </xsd:complexType> -->
  22. <!-- </xsd:element> -->
  23. <!-- 结束:第一种:非包装的方式 -->
  24. <!--开始:第二种:包装的方式 -->
  25. <xsd:sequence minOccurs="1" maxOccurs="unbounded">
  26. <xsd:element name="student" type="tns:studentType"></xsd:element>
  27. </xsd:sequence>
  28. <!-- 结束:第二种:包装的方式 -->
  29. </xsd:sequence>
  30. </xsd:complexType>
  31. <xsd:simpleType name="gradeType">
  32. <xsd:restriction base="xsd:int">
  33. <xsd:minInclusive value="2000" />
  34. <xsd:maxExclusive value="3000" />
  35. </xsd:restriction>
  36. </xsd:simpleType>
  37. </xsd:schema>

schema_student.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="http://www.example.org/schema_classroom"
  4. xmlns:tns="http://www.example.org/schema_classroom"
  5. elementFormDefault="qualified">
  6. <xsd:element name="student" type="tns:studentType"/>
  7. <xsd:complexType name="studentType">
  8. <xsd:sequence>
  9. <xsd:element name="name" type="xsd:string"/>
  10. <xsd:element name="sex" type="tns:sexType"/>
  11. </xsd:sequence>
  12. </xsd:complexType>
  13. <xsd:simpleType name="sexType">
  14. <xsd:restriction base="xsd:string">
  15. <xsd:enumeration value="男"/>
  16. <xsd:enumeration value="女"/>
  17. </xsd:restriction>
  18. </xsd:simpleType>
  19. </xsd:schema>

发表评论

表情:
评论列表 (有 0 条评论,209人围观)

还没有评论,来说两句吧...

相关阅读

    相关 xml Schema

    什么是Schema   Schema(模式) :其作用与DTD一样,也是用于验证XML文档的有效性,只不过它提供了比DTD更强大的功能和更细粒度的数据类型。   另外,

    相关 XMLXML - Schema

    Xml Schema类似于DTD,定义了XML文档的逻辑结构,基本功能如下: a、定义可出现在文档中的元素及元素属性。 b、定义哪个元素是子元素。 c、定义子元素的次