加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService(2)-XML系列之Schema

发布时间:2020-12-16 22:45:50 所属栏目:安全 来源:网络整理
导读:源码下载 :链接:http://pan.baidu.com/s/1o69nBzO?密码: bbw2 一.定义 Schema同样用于检测XML是否符合语法规则。 二.点评 相对DTD而言,有如下优点: 1.语法和Xml相同 2.数据类型很多 3.提供域名机制,就是Java中的包 三.Xml中引入Schema 两种方式: 1.通过

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

一.定义

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

二.点评

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

1.语法和Xml相同

2.数据类型很多

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

三.Xml中引入Schema

两种方式:

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

xml_user_02path.xml

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


xml_user_01namespace.xml

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

</user>

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

xml_user_02path.xml

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


四.Schema的设计方式

3种:

1.RussianDoll

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

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

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

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/schema_1books_RussianDoll" 
	xmlns:tns="http://www.example.org/schema_1books_RussianDoll" 
	elementFormDefault="qualified">
	<element name="books">
		<complexType>
			<!-- unbounded最大是没有限制的 -->
			<sequence maxOccurs="unbounded">
				<element name="book">
					<!-- 复杂类型 -->
					<complexType >
					<attribute name="id" type="int" use="required"></attribute>
						<!-- sequence设置是有序的;minOccurs最小是1;unbounded最大是没有限制的 -->
						<sequence minOccurs="1" maxOccurs="unbounded">
							<element name="bookid" type="int"></element>
							<element name="bookname" type="string"></element>
							<!-- 作者的人数是有选择的:1个或很多 -->
							<choice>
								<element name="auther" type="string"></element>
								<element name="authers" >
									<complexType>
										<!-- unbounded最大是没有限制的 -->
										<sequence maxOccurs="unbounded">
											<element name="auther" type="string"></element>
										</sequence>		
									</complexType>
								</element>
							</choice>
						</sequence>
						
					</complexType>
				</element>
			</sequence>
			
		</complexType>
	</element>
	
</schema>

xml_1books_RussianDoll.xml

<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:book="http://www.example.org/schema_1books_RussianDoll"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:noNamespaceSchemaLocation="schema_1books_RussianDoll.xsd">
	
	<book:book id="1">
		<book:bookid>1</book:bookid>
		<book:bookname>J2EE企业规范图书</book:bookname>
		<book:auther>赵栗婧</book:auther>
	</book:book>
	<book:book id="2"> 
		<book:bookid>2 </book:bookid>
		<book:bookname>J2EE企业规范图书</book:bookname>
		<book:authers>
			<book:auther>赵栗婧1</book:auther>
			<book:auther>赵栗婧2</book:auther>
		</book:authers>
	</book:book>
</book:books>


?2.Salami Slice

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

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

【缺点】根节点不清晰

schema_2books_SalamlSlice.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/schema_2books_SalamlSlice" 
	xmlns:tns="http://www.example.org/schema_2books_SalamlSlice" 
	elementFormDefault="qualified">
	<element name="book" type="tns:bookType"></element>
	<element name="id" type="int"></element>
	<element name="name" type="string"></element>
	<element name="content" type="string"></element>
	<complexType name="bookType">
		<sequence maxOccurs="unbounded">
			<element ref="tns:id"></element>
			<element ref="tns:name"></element>
			<element ref="tns:content"></element>
		</sequence>
	</complexType>
</schema>


xml_2books_SalamlSlice.xml

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

3.CentianBind 推荐

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

schema_3people_venetianbind.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/schema_3people_venetianbind" 
	xmlns:tns="http://www.example.org/schema_3people_venetianbind" 
	elementFormDefault="qualified">
	<element name="people" type="tns:peopleType"></element>
	<complexType name="peopleType">
		<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>
	<!-- 年龄的类型:是int类型的,并且在:1-150岁之间 -->
	<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>
	<!-- 邮件类型的限制 -->
	<simpleType name="emailType">
		<restriction base="string">
<!-- 			<pattern value="(w+.*)*w+@w+.[A-Za-z](2,6)"/> -->
			<minLength value="2"/>
			<maxLength value="255"/>
		</restriction>
	</simpleType>
</schema>


xml_3people_venetianbind.xml

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


五..Schema之间的引用

两种:

1.非包装方式

2.包装方式

schema_classroom.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.example.org/schema_classroom" 
	xmlns:tns="http://www.example.org/schema_classroom"
	elementFormDefault="qualified">
	<!-- 在classroom的schema中引入student的schema -->
	<xsd:include schemaLocation="schema_student.xsd"></xsd:include>
	<!-- 定义元素classroom,类型是classroomType -->
	<xsd:element name="classroom" type="tns:classroomType"></xsd:element>
	<!-- 定义复杂类型:classroomType -->
	<xsd:complexType name="classroomType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="grade" type="tns:gradeType" />
			<!-- 开始:第一种:非包装的方式【建立classroom与student之间的关系】 -->
<!-- 			<xsd:element name="stus"> -->
<!-- 				<xsd:complexType> -->
<!-- 					<xsd:sequence minOccurs="1" maxOccurs="100"> -->
<!-- 						<xsd:element name="student" type="tns:studentType"></xsd:element> -->
<!-- 					</xsd:sequence> -->
<!-- 				</xsd:complexType> -->
<!-- 			</xsd:element> -->
			<!-- 结束:第一种:非包装的方式 -->
			<!--开始:第二种:包装的方式 -->
			<xsd:sequence minOccurs="1" maxOccurs="unbounded">
				<xsd:element name="student" type="tns:studentType"></xsd:element>
			</xsd:sequence>
			<!-- 结束:第二种:包装的方式 -->
		</xsd:sequence>
	</xsd:complexType>
	<xsd:simpleType name="gradeType">
		<xsd:restriction base="xsd:int">
			<xsd:minInclusive value="2000" />
			<xsd:maxExclusive value="3000" />
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>


schema_student.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/schema_classroom"
	xmlns:tns="http://www.example.org/schema_classroom" 
	elementFormDefault="qualified">
	
	<xsd:element name="student" type="tns:studentType"/>
	
	<xsd:complexType name="studentType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string"/>
			<xsd:element name="sex" type="tns:sexType"/>
		</xsd:sequence>
	</xsd:complexType>
	
	<xsd:simpleType name="sexType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="男"/>
			<xsd:enumeration value="女"/>
		</xsd:restriction>
		
	</xsd:simpleType>
</xsd:schema>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读