现在的位置: 首页 > 综合 > 正文

XML模式规范dtd,schema总结

2018年05月20日 ⁄ 综合 ⁄ 共 9651字 ⁄ 字号 评论关闭

===XML===
1.什么是xml?
eXtensible Markup Language 可扩展的标记语言

1)html和xml的区别?
html:
目的:展示,数据和显示方式结合
特点:标记是固定的,不可扩展.
语法宽松,浏览器接受不规范的语法

xml:
目的:数据
特点:标记可扩展
语法严格

2)xml主要用途:
保存数据,交换数据,配置文件

解析器:
html:浏览器
xml:浏览器,myeclipse,xml spy

2.xml语法
1)声明:
<?xml version="1.0" encoding="utf-8"?> 必须出现在文件的开始位置

2)标记:
自定义,有开始标记和结束标记,严格区分大小写
3)根元素:
有且只有一个
4)实体引用:
<: &lt;
>: &gt;
&: &amp;
": &quot;
': &apos;
5)属性
属性一定出现在开始标记
属性必须有值(等号右边必须有内容)

6)CDATA:忽略所有实体引用的纯文本
<![CDATA[本文内容]]>

7)命名空间
<前缀:标记名称>

<f:table>
<f:name>apple</f:name>
<f:price>5</f:price>
</f:table>

<w:table>
<w:name>wood</w:name>
<w:price>100</w:price>
</w:table>

======DTD/Schema=====
----DTD----
1.什么是DTD?  
  Document Type Definition

<计算机书籍>
<书名 bh="123">xxx</书名>
<价格>30</价格>
</计算机书籍>

<Computer>
<bh>123</bh>
<bookname>xxx</bookname>
<price>30</price>
</Computer>

交换数据时,要求xml文件格式相同,所以需要遵守相同的规范.
比如:两份文件要有相同的元素嵌套关系,相同的属性定义,相同的元素顺序,元素出现的次数等.

限制条件:
书籍列表:根元素
计算机书籍:在书籍列表中可以有多个元素
武侠书籍:在书籍列表中可以有多个元素
计算机书籍:书名,作者,价格,简介,必须有序

1)DTD文档的声明及应用
内部DTD文档
<!DOCTYPE 根元素 [定义内容]>

外部的DTD文档
<!DOCTYPE 根元素 SYSTEM "DTD文件(.dtd)路径">

内部使用和外部结合
<!DOCTYPE 根元素 SYSTEM "DTD文件(.dtd)路径" [定义内容]>

2)元素
格式1:<!ELEMENT 标记名称 (子元素名,子元素名....)>

符号:
():用来给元素分组
*:0到多个
,:表示有序
+:表示1到多次
|:表示或
?:表示0或1次

格式2:<!ELEMENT 标记名称 类型>
类型:
(#PCDATA):可以包含任何字符数据,但是不能包含子元素
EMPTY:该元素不能包含子元素和文本,但是可以有属性
ANY:该元素可以包含任何在DTD中定义的元素内容

2)属性
<!ATTLIST 标记名称 属性名称 类型 属性特点>

类型:
CDATA:值为字符数据,可以是中文和数字,字母
NMTOKEN:是CDATA的一个子集,表示属性值必须是英文字母,数字,句号,破折号,下划线,或冒号,属性值中不能含有空格做分隔.
NMTOKENS:与NMTOKEN类似.但可以出现空格.
ID:表示该属性的取值必须是唯一的,并且ID属性值必须是"字母+数字",不能是纯数字

属性特点:
#REQUIRED:该属性在元素中必须出现
#IMPLIED:该属性不是必须的,可有可无
#FIXED "value":属性值是固定的,属性本身可有可无

本质思想就是:树结构;

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT 学生成绩信息 (林科大?,农大?,铁道?)>
<!ELEMENT 林科大 (学生信息)+>
<!ELEMENT 农大 (学生信息)+>
<!ELEMENT 铁道 (学生信息)+>
<!ELEMENT 学生信息 (姓名,性别,成绩表)>
<!ELEMENT 姓名 (#PCDATA)>
<!ELEMENT 性别 (#PCDATA)>
<!ELEMENT 成绩表 (科目)+>
<!ELEMENT 科目 (#PCDATA)>
<!ATTLIST 学生信息 学号 ID #REQUIRED>
<!ATTLIST 学生信息 是否已婚 CDATA #FIXED "true">
<!ATTLIST 科目 名称 CDATA #REQUIRED>

====Schema===

DTD的局限性:
1)DTD不遵守xml的语法
2)DTD数据数据有限(与数据库数据类型不一致)
3)DTD不可扩展
4)DTD不支持命名空间(命名冲突)

Schema的优势:
1)Schema也是基于xml的语法
2)Schema扩充了数据类型,可以自定义数据类型
3)Schema支持元素继承
4)Schema支持属性组

Schema的文档结构:
<xs:schema 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="www.zhongxin.com"
elementFormDefault="qualified" 
attributeFormDefault="unqualified">

xs:schema 根元素
xmlns:xs 验证schema文件本身的命名空间所属
targetNamespace: 当前schema文件的命名空间(自定义)
elementFormDefault:用于指示子元素是否放到命名空间中,值有两种:
qualified:来自目标命名空间的元素必须用命名空间前缀限定.
unqualified:来自目标命名空间的元素不必须用命名空间前缀限定.

attributeFormDefault:用于指示属性是否放到命名空间中,值有两种:
qualified:来自目标命名空间的属性必须用命名空间前缀限定.
unqualified:来自目标命名空间的属性不必须用命名空间前缀限定.

1.元素定义:
一般格式
<xs:element name="book" type="xs:int"></xs:element>

element
常用属性:
name:定义的元素名称
type:定义元素的数据类型
-常用的简单数据类型:
 xs:string xs:int xs:boolean xs:date xs:time
 xs:decimal 任意精度 xs:double 双精度

ref:必须指向一个全局元素
default:为元素定义默认值(仅当元素是简单类型或textOnly时使用)
maxOccurs:值表示该元素在父元素中出现的最大次数,它的取值为大于或等于0
 默认值为unbounded.
minOccurs:值表示该元素在父元素中出现的最小次数,它的取值为大于或等于0
 默认值为1.

--xml中引用Schema文件:
有命名空间:
<my:book xmlns="www.zhongxin.com" 
xmlns:my="www.zhongxin.com"
xsi:schemaLocation="www.zhongxin.com firstSchema.xsd">
无命名空间: 
xsi:noNamespaceSchemaLocation="firstSchema.xsd"

fixed:为简单元素设置固定值
注意: 在元素中的值不能有空格

2.属性定义:
<xs:attribute name="id" type="xs:string"/>
attribute主要属性:name,default,fixed,ref,type

use:指示元素该如何使用该attribute类型
值有:
optional(默认):表示该属性可以是任何值,属性可选
prohibited:表示不能使用该属性
required:表示该属性必须出现一次

3.SimpleType类型元素:(不能包含子元素或属性)
用于xml元素或者属性中定义可接受的值
方式有内嵌和应用两种

注意:
a.内嵌的时候不能使用类型名
b.推荐使用引用类型,方便扩展,但注意是否有nameSpace="xx",有的话加上
    xmlns="xx"

1)引用类型基本使用:
<xs:element name="ageTest" type="ageType"/>
<xs:simpleType name="ageType">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="200"/>
</xs:restriction>
</xs:simpleType>
restriction元素:
属性:base属性指定数据类型
子元素:
minInclusive:最小值(包含边界)
maxInclusive:最大值(包含边界)
minExclusive:最小值(不包含边界)
maxExclusive:最大值(不包含边界)
pattern:主要用于对某个属性或元素进行约束,可以使用正则表达式
enumeration:元素的内容限制为一组可接受的值

简单类型的例子:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns="www.zhongxin.com" targetNamespace="www.zhongxin.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<!--内嵌
	<xs:element name="age">
		<xs:simpleType>
			<xs:restriction base="xs:int">
				<xs:minExclusive value="0"/>
				<xs:maxExclusive value="200"/>
			</xs:restriction>
		</xs:simpleType>
	</xs:element>
	-->
	<!--引用方式
	<xs:element name="ageTest" type="ageType"/>
	<xs:element name="ageTest1" type="ageType"/>
	<xs:simpleType name="ageType">
		<xs:restriction base="xs:int">
			<xs:minInclusive value="0"/>
			<xs:maxInclusive value="200"/>
		</xs:restriction>
	</xs:simpleType>
	-->
	<xs:element name="id" type="idType"/>
	<xs:simpleType name="idType">
		<xs:restriction base="xs:string">
			<xs:pattern value="[A-Za-z]{3}-\d{3}"/>
		</xs:restriction>
	</xs:simpleType>
	
	<xs:element name="car" type="carType"/>
	<xs:simpleType name="carType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="奥迪"/>
			<xs:enumeration value="玛莎拉蒂"/>
			<xs:enumeration value="雷克萨斯"/>
			<xs:enumeration value="吉利"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

4.复杂类型
复杂类型是可以包含其他元素,属性和组的元素定义
1)sequence:要求一组中元素以指定的顺序出现
属性:
maxOccurs:定义最多出现的次数
minOccurs:定义最少出现的次数

2)all:一组元素可以无序出现
maxOccurs:定义最多出现的次数
minOccurs:定义最少出现的次数

案例:

1.仅包含子元素的元素

2.含文本和属性的复杂元素
<xs:complexType name="peoType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" use="required"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>
simpleContent:用于对complexType元素或字符数据做约束.但是不能包含子元素
子元素:
extension:扩展simpleType的元素内容
restriction:限制数据,simpleType元素的子集

3.包含文本,子元素和属性的复杂元素
<xs:complexType name="peoType">
<xs:attribute name="idCard" type="xs:string" use="required"/>
</xs:complexType>
注意:
    a.使用sequence或all在设置属性attribute之前
    b.mixed="true":表示是字符数据可以出现在子元素之间

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="www.zhongxin.com" targetNamespace="www.zhongxin.com"
 elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="peoples" type="peoplesType"/>
	<xs:complexType name="peoplesType">
		<xs:sequence minOccurs="1" maxOccurs="unbounded">
			<xs:element name="people" type="peopleType1"/>
			<xs:element ref="peo"/>
		</xs:sequence>
	</xs:complexType>
	<!--有序出现-->
	<xs:complexType name="peopleType">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="age" type="ageType"/>
			<xs:element name="car" type="carType"/>
		</xs:sequence>
	</xs:complexType>
	<!--无序出现,有属性idCard,通过设置mixed="true"标签之中可以出现文本-->
	<xs:complexType name="peopleType1" mixed="true">
		<xs:all>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="age" type="ageType"/>
			<xs:element name="car" type="carType"/>
			<xs:element ref="class"/>
		</xs:all>
		<xs:attribute name="idCard" use="required"/>
	</xs:complexType>
	<xs:simpleType name="ageType">
		<xs:restriction base="xs:int">
			<xs:minInclusive value="0"/>
			<xs:maxInclusive value="200"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="carType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="奥迪"/>
			<xs:enumeration value="玛莎拉蒂"/>
			<xs:enumeration value="雷克萨斯"/>
			<xs:enumeration value="吉利"/>
		</xs:restriction>
	</xs:simpleType>
	
	<!--含文本和属性的复杂元素-->
	<xs:element name="peo" type="peoType"/>
	<xs:complexType name="peoType">
		<xs:simpleContent>
			<xs:extension base="xs:string">
				<xs:attribute name="id" type="xs:string" use="required"/>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	
	<!--定义教室标签中文本数据简单类型限定-->
	<xs:simpleType name="simpleType1">
		<xs:restriction base="xs:string">
			<xs:enumeration value="small"/>
			<xs:enumeration value="middle"/>
			<xs:enumeration value="big"/>
		</xs:restriction>
	</xs:simpleType>
	
	<!--对简单类型做数据限定
	<xs:simpleType name="simpleType2">
		<xs:restriction base="simpleType1">
			<xs:enumeration value="middle"/>
			<xs:enumeration value="big"/>
		</xs:restriction>
	</xs:simpleType>
	-->
	
	<xs:element name="class" type="class1"/>	
	<xs:complexType name="class">
		<xs:simpleContent>
			<!--通过extension扩展属性-->
			<xs:extension base="simpleType1">
				<xs:attribute name="id" type="xs:string" use="required"/>
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	
	<xs:complexType name="class1">
		<xs:simpleContent>
			<!--数据限定,只能限定复杂类型-->
			<xs:restriction base="class">
				<xs:enumeration value="middle"/>
				<xs:enumeration value="big"/>
			</xs:restriction>
		</xs:simpleContent>
	</xs:complexType>
</xs:schema>

3.choice选择器
规定可出现一组元素的某一个.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="www.zhongxin.com" targetNamespace="www.zhongxin.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="authors" type="asType"/>
	<xs:complexType name="asType">
		<xs:sequence>
			<xs:element name="author" type="aType"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="aType">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="age" type="xs:int"/>
			<xs:choice>
				<xs:element name="phone" type="xs:string"/>
				<xs:element name="tel" type="xs:string"/>
			</xs:choice>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

4.group组
group声明内部定义一个all,choice,sequence元素

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="students" type="stusType"></xs:element>
	<xs:complexType name="stusType">
		<xs:sequence maxOccurs="unbounded">
			<xs:element name="student" type="stuType"/>
		</xs:sequence>
	</xs:complexType>
	<xs:complexType name="stuType">
		<xs:sequence>
			<xs:group ref="stuGro"/>
			<xs:element name="school" type="xs:string"/>
		</xs:sequence>
		<xs:attributeGroup ref="attrGro"/>
	</xs:complexType>
	<!--定义元素组信息-->
	<xs:group name="stuGro">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="age" type="xs:int"/>
			<xs:element name="sex" type="sexType"/>
		</xs:sequence>
	</xs:group>
	<!--定义属性组信息-->
	<xs:attributeGroup name="attrGro">
		<xs:attribute name="id" type="xs:string" use="required"/>
		<xs:attribute name="idCard" type="xs:string"/>
	</xs:attributeGroup>
	<xs:simpleType name="sexType">
		<xs:restriction base="xs:string">
			<xs:enumeration value="男"/>
			<xs:enumeration value="女"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

5.ComplexContent:
定义对复杂类型的扩展或限定

层次关系总结:

Grounp

simpleType

ComplexType

all
无序

sequence 有序

choice
选择之一 

simpleContent

conmplexContent

restriction限制

extension   扩展

【上篇】
【下篇】

抱歉!评论已关闭.