早期出现的一种XML约束模式语言根据它的语法创建的文件称为DTD文件。
1.DTD约束文档
//表示书架元素中有一个或者多个书元素
<!ELEMENT 书架 (书+)>
//表示元素书包含书名、作者、售价这三个子元素,并且这些子元素要按照顺序依次出现
<!ELEMENT 书 (书名,作者,售价)>
//书名、作者和售价都是元素名称
//(#PCDATA)表示元素中嵌套的内容是普通的文本字符串。
<!ELEMENT 书名 (#PCDATA)>
<!ELEMENT 作者 (#PCDATA)>
<!ELEMENT 售价 (#PCDATA)>
1.引入DTD文件
本地DTD文件
公共的DTD文件
内嵌式引入DTD文件:
2.元素定义
语法格式
元素内容的五种形式
#PCDATA:表示普通文本字符串
子元素:说明元素包含内容,使用一对圆括号()表示
混合内容:包含字符数据和子元素
EMPTY:没有内容,但有属性
元素内容中包含的符号
[?],该对象可以出现0次或1次
[*],该对象可以出现0次或多次
[+],该对象可以出现1次或多次
[|],表示列出对象中选择一个
[,],该对象按照指定顺序出现
[()],用于给元素分组
3.属性定义
语法格式
<!ATTLIST 元素名
属性名1 属性类型 设置说明
属性名2 属性类型 设置说明
>
设置说明
#REQUIRED:required,属性是必须的。
#IMPLIED:impled,属性可有可无。
#FIXED:fixed,一个固定的属性默认值
默认值:可改变的默认值。
属性类型
CDATA:字符数据 注意转义字符
Enumerated(枚举类型):从列表中选择
ID:唯一标识XML文档中的一个元素,不能重复
ID类型的属性必须设置为#REQUIRED或者#IMPLIED,且无法设置默认值
IDREF和IDREFS:一对一和一对多的关系
4.实体定义
//dtd约束文件book.dtd
<!ELEMENT books (book+) >
<!ELEMENT book (bookname,author,price) >
<!ELEMENT bookname (#PCDATA) >
<!ELEMENT author (#PCDATA) >
<!ELEMENT price (#PCDATA) >
<!ATTLIST price
unit (yuan|USD) "yuan"
>
//dtd约束下的xml文件,book_dtd.xml
<?xml version="1.0"?>
<!DOCTYPE books SYSTEM "book.dtd">
<books>
<book>
<bookname>javaweb</bookname>
<author>Xiaoxi</author>
<price unit="USD">38.00</price>
</book>
<book>
<bookname>javaweb</bookname>
<author>Xiaoxi</author>
<price unit="USD">38.00</price>
</book>
</books>
用于定义和描述XML文档结构与内容的模式语言,比DTD功能强大,语法复杂。
1.名称空间
语法格式
<元素名 xmlns:前缀名=“URI”>
<?xml version="1.0" encoding="UTF-8"?>
//根元素xs:schema的属性都在http://w3.org/2001/XMLSchema名称空间中,因此,在根元素上必须声明该名称空间
//名称空间可以唯一标识一个元素或者属性
<it315:书架 xmlns:it315="http://www.it315.org/xmlbook/schema">
<it315:书>
<it315:书名>JavaScript网页开发</it315:书名>
<it315:作者>张孝祥</it315:作者>
<it315:售价>28.00元</it315:售价>
</it315:书>
</it315:书架>
2.引入Schema约束
使用名称空间引入XML Schema文档
通过属性xsi:schemaLocation来声明名称空间的文档,xsi:schemaLocation属性是在标准名称空间“http://www.w3.org/2001/XMLSchema-instance”中定义的。
<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns="http://www.it315.org/xmlbook/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.it315.org/xmlbook/schema
http://www.it315.org/xmlbook.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
不使用名称空间引入XML Schema文档
通过xsi:noNamespaceSchemaLocation属性直接指定,noNamespaceSchemaLocation属性是在标准名称空间“http://www.w3.org/2001/XMLSchema-instance”中定义的。
<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xmlbook.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
3.元素的定义
语法格式
< xs:element name=“元素名称” type=“数据类型”/>
常用数据类型
xs:string:表示字符串类型 ; xs:decimal:表示小数类型
xs:integer:表示整数类型;xs:boolean:表示布尔类型
xs:date:表示日期类型;xs:time:表示时间类型
<xs:element name=“lastname” type=“xs:string” / >
<xs:element name=“age” type=“xs:integer” / >
<xs:element name=“dateborn” type=“xs:date” / >
<lastname >Smith</lastname>
<age>28</age>
<dateborn>1980-03-27</dateborn>
4.属性的定义
语法格式
<xs:attribute name=“lang” type=“xs:string” / >
5.简单类型的限定:只包含字符数据的元素都是简单类型
xs:simpleType元素
定义简单类型
xs:restriction元素
对现有元素内容的类型进行限制
xs:minInclusive和xs:maxInclusive元素
对值的限定,最大值和最小值
<xs:element name=“age” >
<xs:simpleType>
<xs:restriction base=“xs:interger”>
<xs:minInclusive value=“18” />
<xs:maxInclusive value=“58”/>
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:enumeration元素
为元素的内容限制一组可接受的值
//要限定一个元素名为Car的元素,可接受的值只有:Audi、Golf、BMW
<xs:element name=“car” >
<xs:simpleType>
<xs:restriction base=“xs:string”>
<xs:enumeration value=“Audi” />
<xs: enumeration value=“Golf”/>
<xs: enumeration value=“BMW”/>
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:pattern元素
为元素内容限制定义为一系列可使用的数字或字母,可以使用模式约束
//要定义一个带有限定的元素“letter”,要求可接受的值只能是字母a-z其中一个
<xs:element name=“letter” >
<xs:simpleType>
<xs:restriction base=“xs:string”>
<xs:pattern value=“{a-z}” />
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:whiteSpace元素
对空白字符的限定
属性
preserve(不对元素中的任何空白字符进行处理)
replace(移除所有的空白字符)
collapse(将所有的空白字符缩减为一个单一字符)
<xs:element name=“address” >
<xs:simpleType>
<xs:restriction base=“xs:string”>
<xs:whiteSpace value=“preserve” />
</xs:restriction>
</xs:simpleType>
</xs:element>
6.复杂类型的限定:除简单类型之外的其它类型都是复杂类型
xs:complexType元素
定义复杂类型
复杂类型分类
- 空元素(不包含内容,只包含属性):
< product prodid=“1345” />
//空元素在XML Schema文档中对应的定义方式
<xs:element name=“product” >
<xs:complexType>
<xs:attribute name=“prodid” type=“xs:positiveInteger” />
</xs:complexType>
</xs:element>
- 包含其它元素的元素
//元素person嵌套了两个元素
<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>
- 仅包含文本和属性的元素
<shoesize country=“france” >35</shoesize>
<xs:element name=“shoesize” >
<xs:complexType>
//对于仅含文本的复合元素,需要使用simpleContent元素来添加内容,对内容进行扩展或限定
//extension或restriction元素来扩展或限制元素
<xs:simpleContent>
<xs:extension base=“xs:integer” >
<xs:attribute name=“country” type=“xs:string” />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
- 包含元素和文本的元素
<letter >
Dear Mr.<name>John Smith</name>
Your order <ordered>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
//在Schema文档中对应的定义方式
//mixed属性用来规定是否允许字符数据出现在复杂类型的子元素之间,默认情况下mixed的值为false
<xs:element name=“letter” >
<xs:complexType mixed=“true”>
<xs:sequence>
<xs:element name=“name” type=“xs:string” />
<xs:element name=“orderid” type=“xs:positiveInteger” />
<xs:element name=“shipdate” type=“xs:date” />
</xs:sequence>
</xs:complexType>
</xs:element>
7.实体定义
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd">
<book>
<bookname>javaweb</bookname>
<author>xiaoming</author>
<price util="USD">30.00</price>
</book>
<book>
<bookname>javaweb</bookname>
<author>xiaoming</author>
<price util="USD">30.00</price>
</book>
</book>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="books">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded" minOccurs="1">
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bookname" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute default="yuan" name="unit">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="yuan"/>
<xsd:enumeration value="USD"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>