Download - Xsd培训资料
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:boolean
xs:date
xs:time
Xs: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”>
<element name="publicList"><complexType>
<sequence> <element type=“string“/></sequence>
</complexType></element>
...
指示器 Order 指示器
All Choice Sequence
Occurrence 指示器 maxOccurs minOccurs
Group 指示器 Group name attributeGroup name
数据约束我们如何对 XML 中的数据进行限制呢?
数据类型的限定限定 描述
enumeration 定义可接受值的一个列表fractionDigits 定义所允许的最大的小数位数。必须大于等于 0 。
length定义所允许的字符或者列表项目的精确数目。必须大于或等于0 。
maxExclusive 定义数值的上限。所允许的值必须小于此值。maxInclusive 定义数值的上限。所允许的值必须小于或等于此值。
maxLength定义所允许的字符或者列表项目的最大数目。必须大于或等于0 。
minExclusive 定义数值的下限。所允许的值必需大于此值。minInclusive 定义数值的下限。所允许的值必需大于或等于此值。
minLength定义所允许的字符或者列表项目的最小数目。必须大于或等于0 。
pattern 定义可接受的字符的精确序列。totalDigits 定义所允许的阿拉伯数字的精确位数。必须大于 0 。
whiteSpace 定义空白字符(换行、回车、空格以及制表符)的处理方式。
对值的限定 对数值的限定
限定 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>