`
schy_hqh
  • 浏览: 543165 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

XML_Schema

 
阅读更多
Schema的好处
schema出现的目的:通过一个更加合理的方式来编写xml的限制文件,而且是基于xml语法的方法
使用Schema来规范限制xml的编写

schema通过命名空间来支持多个名称相同的元素
    命名空间:避免元素间的冲突,比如同名元素(类似于java中package的作用)

schema可以很好的完成对java或者所有对象的修饰,并且提供了大量的数据类型来定义元素

命名空间
01.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	1. xmlns 指定命名空间(默认的标准命名空间,一个标准,由eclipse自动已经加载了)由于是默认的命名空间,其中的所有元素可以直接访问,不用加前缀修饰
	2. xmlns:tns 指定额外的命名空间,需要加前缀修饰才能访问到(外部通过tns前缀来访问本schema中定义的元素 )
		 (比如,要访问http://www.example.org/01位置对于的schema就要使用<tns:xx></tns:xx>来指名) 
	3. 不加前缀的命名空间即为默认命名空间,只能存在一个默认命名空间
	4.targetNamespace 指定通过目标命名空间的位置,外部只要通过这个地址就能引用到对应的schema(外部通过这个地址来定位本schema)
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/01"
		xmlns:tns="http://www.example.org/01"
		elementFormDefault="qualified">
		
	<!-- 定义user对象的schema -->
	<element name="user">
		<!-- user为复杂类型 -->
		<complexType>
			<sequence>
				<element name="id" type="ID"/>
				<element name="uname" type="string"/>
				<element name="born" type="dateTime"/>
			</sequence>
		</complexType>
	</element>
	
</schema>


将schema加入到eclipse的目录册中
window-->preference-->XML-->xml catalog-->User Specified Entries-->add
Catalog Entry-->
Location:workspace或者文件目录中找到对应的xsd文件
Key type:System ID
Key:自动加载



引入schema,编写xml
通过targetNamespace引入schema(default namespace)
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
引入schema 
	1.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 获取schema的一个实例,通过它可以引入其他的schema 
	2.xsi:schemaLocation="http://www.example.org/01" 通过实例引入一个指定targetNamespace的schema
	3.将引入的schema作为本xml的默认命名空间,就可以直接访问其中定义好的元素了
-->
<user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:schemaLocation="http://www.example.org/01"
	  xmlns="http://www.example.org/01">
	<id>1</id>
	<uname>zhangsan</uname>
	<born>1999-09-09</born>
</user>


通过xsd的路径引入schema(location)
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
引入schema 
	1.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 获取schema的一个实例,通过它可以引入其他的schema 
	2.xsi:noNamespaceSchemaLocation="01.xsd" 通过xsd文件的路径来引入,而不是使用schema的targetNamespace引入的
	3.将引入的schema作为本xml的默认命名空间,就可以直接访问其中定义好的元素了
-->
<user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:noNamespaceSchemaLocation="01.xsd"
	  xmlns="http://www.example.org/01">
	  
	<id>2</id>
	<uname>lisi</uname>
	<born>2000-11-11</born>
	
</user>



--------------------------------------------------------
schema中控制元素
02.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/02"
		xmlns:tns="http://www.example.org/02" 
		elementFormDefault="qualified">

	<element name="books">
		<complexType>
			<sequence maxOccurs="unbounded">
				<element name="book">
					<complexType>
					
						<!-- 给book定义元素 -->
						<!-- sequence中定义的元素在xml中需要按顺序编写 -->
						<!-- minOccurs最少出现次数 maxOccurs最大出现次数 (unbounded无限的) -->
						<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>
										<!-- 最少1个作者,最多5个作者 -->
										<sequence minOccurs="1" maxOccurs="5">
											<element name="author" type="string" />
										</sequence>
									</complexType>
								</element>
							</choice>
						</sequence>
						
						<!-- 给 book定义属性(注意:属性定义需要放在sequence之后) -->
						<attribute name="id" type="int" use="required"/>
						
					</complexType>
				</element>
			</sequence>
		</complexType>
	</element>

</schema>

02.xml
<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xsi:schemaLocation="http://www.example.org/02"
	  xmlns:book="http://www.example.org/02">
	
	<book:book id="1">
		<book:title>Think in Java</book:title>
		<book:content>java is good</book:content>
		<book:author>foreigner</book:author>
	</book:book>
	  
	<book:book id="2">
		<book:title>IT</book:title>
		<book:content>XXXX</book:content>
		<book:authors>
			<book:author>YY</book:author>
		</book:authors>
	</book:book>  
	
</book:books>



-------------------------------------


schema的几种编写方式

1.Russian Doll

只有一个根元素,通过嵌套的方式完成编写。
优点:根元素只有一个,结构清晰,方便定位。
缺点:无法实现类型的重用。
如,02.xsd


2.Salami Slice

缺点:有多个根元素,结构非常不清晰
优点:最大化实现类型重用

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/03"
		xmlns:tns="http://www.example.org/03" 
		elementFormDefault="qualified">

	<!-- 
		指名使用tns命名空间中的bookType
		由于默认命名空间是http://www.w3.org/2001/XMLSchema
		因此,在访问其它命名空间中定义的元素时,就需要加收前缀,比如本schema的前缀为tns
	-->
	<element name="book" type="tns:bookType"></element>
	<element name="id" type="int"></element>
	<element name="title" type="string"></element>
	<element name="content" type="string"></element>
	
	<complexType name="bookType">
		<sequence>
			<element ref="tns:id"/>
			<element ref="tns:title"/>
			<element ref="tns:content"/>
		</sequence>
	</complexType>
	
</schema>


3.Venetian Blind 百叶窗(推荐)

综合上面2种编写方式的优点而成

04.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.example.org/04"
		xmlns:tns="http://www.example.org/04" 
		elementFormDefault="qualified">
	
	<!-- 只有一个根元素 -->
	<element name="person" type="tns:personType"></element>
	
	<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专用于定义类型,实现类型的重用 -->
	
	<simpleType name="ageType">
		<!-- 基于age的约束 -->
		<restriction base="int">
			<!-- 最小包含1 -->
			<minInclusive value="1"></minInclusive>
			<!-- 最大包含120 -->
			<maxInclusive value="120"></maxInclusive>
		</restriction>
	</simpleType>
	
	<simpleType name="sexType">
		<restriction base="string">
			<enumeration value="男"></enumeration>
			<enumeration value="女"></enumeration>
			<enumeration value="未知"></enumeration>
		</restriction>
	</simpleType>
	
	<simpleType name="emailType">
		<restriction base="string">
			<!-- 
				正则表达式验证 
				^ 开始
				* 0或多次
				? 0或1次
				+ 1或多次
				\. 转义后表示本身,即.
				$ 结尾
			-->
			<pattern value="\w+[-_]?\w+@\w+\.[A-Za-z]{2,3}"></pattern>
			<minLength value="6"></minLength>
			<maxLength value="20"></maxLength>
		</restriction>
	</simpleType>
	
</schema>


04.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:noNamespaceSchemaLocation="04.xsd"
		xmlns="http://www.example.org/04"
		sex="未知">
		
	<name>wahaha</name>
	<age>120</age>
	<email>schy_hqh@def.hik</email>
	
</person>


使用多个schema完成一个任务(schema的重用)
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
		xmlns:stu="http://www.example.org/classroom" 
		targetNamespace="http://www.example.org/classroom"
		elementFormDefault="qualified">
	<!-- 
		注意:
		1.在student.xsd中,命名空间前缀为stu
		要想被class.xsd引用的话,前缀不能相同,否则会报错,所以在student.xsd中,
		引用自己定义的类型时,通过stu前缀来访问
		2.student.xsd中定义的stuType要想在class.xsd中被重用,
		将命名空间设置为与class.xsd的命名空间一致,都为http://www.example.org/classroom
	 -->	
	<xsd:element name="student" type="stu:stuType"/>
	<!-- 学生类型 -->
	<xsd:complexType name="stuType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="sex" type="stu:sexType" />
		</xsd:sequence>
	</xsd:complexType>
	<!-- 性别类型 -->
	<xsd:simpleType name="sexType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="男"></xsd:enumeration>
			<xsd:enumeration value="女"></xsd:enumeration>
			<xsd:enumeration value="未知"></xsd:enumeration>
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>


class.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	targetNamespace="http://www.example.org/classroom" xmlns:tns="http://www.example.org/classroom"
	elementFormDefault="qualified">
	
	<!-- 引入student.xsd -->
	<xsd:include schemaLocation="student.xsd"></xsd:include>
	 <!--
	 import该怎样使用才能引用到student.xsd中定义的stuType呢???
	 <xsd:import namespace="http://www.example.org/student" schemaLocation="student.xsd" />
	 -->
	<xsd:element name="classroom" type="tns:classroomType"/>
		
	<xsd:complexType name="classroomType">
		<xsd:sequence>
			<xsd:element name="grade" type="tns:gradeType"/>
			<xsd:element name="name" type="xsd:string"/>
			<!-- 
				第一种方式:
				将一组学生包装包stus中,这样通过xjc转换的java文件classroom。java中存在内部类 -->
			<xsd:element name="stus">
				<xsd:complexType>
					<xsd:sequence minOccurs="1" maxOccurs="unbounded">
						<!-- 使用student.xsd中定义好的stuType,由于student中的命名空间与class.xsd一致,所以这里才能通过tns前缀访问到stuType -->
						<xsd:element name="stu" type="tns:stuType"/>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<!-- 第二种方式:直接将学生单独一个一个列出来 
			<xsd:sequence minOccurs="1" maxOccurs="unbounded">
				<xsd:element name="stu" type="tns:stuType"/>
			</xsd:sequence>
			-->
		</xsd:sequence>
	</xsd:complexType>		
		
		<xsd:simpleType name="gradeType">
			<xsd:restriction base="xsd:int">
				<xsd:minInclusive value="2013"/>
				<xsd:maxInclusive value="2017"/>
			</xsd:restriction>
		</xsd:simpleType>
</xsd:schema>



使用classroom.xsd结合student.xsd完成classroom.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  classroom.xsd">
	<grade>2013</grade>
	<name>room one</name>
	<stus>
		<stu>
			<name>zs</name>
			<sex>男</sex>
		</stu>
		<stu>
			<name>ls</name>
			<sex>未知</sex>
		</stu>
	</stus>
</classroom>




使用xjc(xml java change)将schema转换为java文件
cmd命令行:

将src目录下的classroom.xsd转为java放入xjc_01目录中
C:\Users\lenovo>xjc -d E:\technology-hqh\proj\webservice\JAX-WS\xjc_01 E:\technology-hqh\proj\webservice\JAX-WS\schema\src\classroom.xsd

parsing a schema...
compiling a schema...
org\example\classroom\ClassroomType.java
org\example\classroom\ObjectFactory.java
org\example\classroom\SexType.java
org\example\classroom\StuType.java
org\example\classroom\package-info.java

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics