XSDXML数据文件的格式校验
概述XSD是干什么的呢?XSD是个什么样子的呢?XSD怎么使用呢?
XSD的作用Look!
XSD的主要作用Use!我们可以用它来做什么?格式校验数据校验我们什么时间用它呢?存储前...转换前...
Schema的样子<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 	targetNamespace="http://www.xinaomdt.com/hie/1.0" 	xmlns="http://www.xinaomdt.com/hie/1.0" 	elementFormDefault="qualified">   <xs:element name="note">    <xs:complexType>      <xs:sequence>         <xs:element name="to" type="xs:string"/>         <xs:element name="from" type="xs:string"/>         <xs:element name="heading" type="xs:string"/>         <xs:element name="body" type="xs:string"/>       </xs:sequence>     </xs:complexType>   </xs:element> </xs:schema>
XSD的引用<?xml version="1.0"?><note xmlns="http://www.xinaomdt.com/hie/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xinaomdt.com/hie/1.0 note.xsd">	<to>George</to> 	<from>John</from> 	<heading>Reminder</heading> 	<body>Don't forget the meeting!</body> </note>
数据类型在XSD中都支持什么数据类型呢?
数据类型的支持描述允许的文档内容 验证数据的正确性 定义数据约束(data facets) 定义数据模型(或称数据格式) 在不同的数据类型间转换数据
内建数据类型常用的内建数据类型xs:string xs:decimal xs:integer xs:booleanxs:date xs:timeXs:dateTime
简单数据类型
复杂数据类型示例<person><firstname>John</firstname><lastname>Smith</lastname></person>对应的Schema<xs:element name="person" type="persontype"/> <xs:complexType name="persontype"> 	<xs:sequence> 		<xs:element name="firstname" type="xs:string"/> 		<xs:element name="lastname" type="xs:string"/> 	</xs:sequence> </xs:complexType>
元素XSD是由什么组成的呢?
简单元素简易元素指那些仅包含文本的元素;它不会包含任何其他的元素或属性;文本的类型可以是内建数据类型,也可以是自定义数据类型;可以增加数据约束(facets);
定义简单元素定义简单元素的语法<xs:element name=“elementName" type=“BuiltInTypeOrCustomType"/>简单元素示例<lastname>Smith</lastname> <age>28</age> <dateborn>1980-03-27</dateborn> 对应的简单元素定义:<xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>
复杂元素示例<person><firstname>John</firstname><lastname>Smith</lastname></person>对应的Schema<xs:element name="person"><xs:complexType><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/></xs:sequence></xs:complexType></xs:element>
类型的继承<xs:complexType name="personinfo">	<xs:sequence>		<xs:element name="firstname" type="xs:string"/>		<xs:element name="lastname" type="xs:string"/>	</xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo">	<xs:complexContent>		<xs:extension base="personinfo"><xs:sequence>			<xs:element name="address" type="xs:string"/>			<xs:element name="city" type="xs:string"/>			<xs:element name="country" type="xs:string"/></xs:sequence>		</xs:extension>	</xs:complexContent></xs:complexType>
专业术语标准的XSD文件是怎么形成的?
以一个例子作为开始<?xml version="1.0" encoding="utf-8"?><router xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><node id="EC6" type="filepoller" x="217" y="122" label="文件轮询器" icon="images/filepoller.png"><publiclist><property name="service" label="服务名称" value="service002" valueType="String“/>      <property name=“endpoint” label=“端点” value=“endpoint002” valueType=“String”/>      <property name="file" label="轮询文件夹" value="d:/HR/CLINIC/XDS" valueType="String”/>     <property name="period" label="轮询周期(ms)" value="20000" valueType="int”/>      <property name=“archive” label=“归档文件夹” value=“d:/HR/CLINIC/backup” valueType=“String”/>     <property name="deleteFile" label="是否删除原始文件" value="true" valueType="boolean”/>     <property name="maxSize" label="过滤文件大小(kb)" value="20480" valueType="int”/>    </publiclist>  </node>  <node id="EC8" type="jmsprovider" x="34" y=“29" label="JMS提供者" icon="images/jmsprovider.png">    <publiclist>      <property name="service" label="服务名称" value="service001" valueType="String“/>      <property name="endpoint" label="端点" value="endpoint001" valueType="String”/>      <property name="connectionFactory" label="连接工厂" value="jms_1" valueType="jms”/>    </publiclist>  </node>  <line id="EL3" type="null” label="路由线" startX="250" startY="130" endX="353" endY="150" startNodeID="EC6" startType="outlet" endNodeID="EC8" endType="inlet"/>  <resources>    <resource id="jms_1”>      <property name="constructor-arg" value="tcp://10.4.9.201:44444"/>      <property name="destinationName" value="hie.HRA0001"/>    </resource>  </resources></router>
命名空间提供避免元素命名冲突的方法既可以放置在根元素上,也可以放在子元素上	xmlns:hie=“http://www.xinaomdt.com/hie/1.0”默认命名空间   为某个元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。
目标命名空间这个是XSD独有的,XML是没有的;缺省命名空间和目标命名空间的区别:<xs:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.xinaomdt.com/hie/router/1.0”><elementname="publicList"><complexType><sequence>  <element type=“string“/></sequence></complexType></element>...
指示器Order指示器All Choice Sequence Occurrence 指示器maxOccurs minOccurs Group指示器Group name attributeGroup name
数据约束我们如何对XML中的数据进行限制呢?
数据类型的限定
对值的限定对数值的限定限定 120 >= Age >= 0<xs:simpleType>	<xs:restriction base="xs:integer">		<xs:minInclusive value="0"/>		<xs:maxInclusive value="120"/>	</xs:restriction></xs:simpleType>限定 120> Age > 0<xs:simpleType>	<xs:restriction base="xs:integer">		<xs:minExclusive value="0"/>		<xs:maxExclusive value="120"/>	</xs:restriction></xs:simpleType>
枚举值的限定将值的选择范围限定一个列表中下面定义了一个汽车型号的类型<xs:simpleType name="carType">	<xs:restriction base="xs:string"> 		<xs:enumeration value="Audi"/> 		<xs:enumeration value="Golf"/> 		<xs:enumeration value="BMW"/> 	</xs:restriction> </xs:simpleType>
对长度的限定使用length进行限制	<xs:simpleType>		<xs:restriction base="xs:string">			<xs:length value="8"/>		</xs:restriction>	</xs:simpleType>使用minLength、MaxLength进行限制<xs:simpleType>		<xs:restriction base="xs:string">			<xs:minLength value="5"/>			<xs:maxLength value="8"/>		</xs:restriction></xs:simpleType>
正则表达式的限定将内容限制定义为一系列可使用的数字或字母;在XSD中进行正则表达式限定叫做模式约束(pattern);示例Schema:<xs:simpleType>	<xs:restriction base="xs:string"> 		<xs:pattern value="[a-z]"/> 	</xs:restriction> </xs:simpleType>
默认值和固定值简单类型是可以支持默认值和固定值的。默认值当没有其他的值被规定时,默认值就会自动分配给元素。<xs:element name="color" type="xs:string" default="red"/> 	<xs:attribute name="color" type="xs:string" default="red"/>固定值固定值同样会自动分配给元素,并且您无法规定另外一个值。	<xs:element name="color" type="xs:string" fixed="red"/>	<xs:attribute name="color" type="xs:string" fixed="red"/>
空值的表示对于属性:使用属性 use="optional/required"对于元素:使用属性 nillable="true/false"使用属性 minOccurs="0"空值示例Schema<complexType name="TypeWithNullElements">	<sequence>		<element name="nillableElem" nillable="true" type="int"/>		<element name="minOccursElem" minOccurs="0" type="int"/>	</sequence></complexType>
空值的示例带数值的元素<typeWithNullElements>	<nillableElem>5</nillableElem>	<minOccursElem>5</minOccursElem></typeWithNullElements>不带数值的元素<typeWithNullElements>	<nillableElem xsi:nil="true"/></typeWithNullElements>

Xsd培训资料