XML Schema Part 1 – Week 3
为什么需要用Schema?
方便扩展,可以定义新数据类型,自文档化,方便表示XML
XML Doc 例子
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="flatBook.xsd">
<author>John Howard</author>
<editor> George W Bush</editor>
<title>Memoir of Saddam</title>
</book>
XML Schema 例子
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string"/>
<xs:element name="editor" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
附加有Schema的文档
XML document entry:
<?xml version="1.0" encoding="UTF-8"?>
<bookshop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="D:/subject/2003/IR/Examples/bookshopLocal.xsd">
XML Schema entry:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
元素内容模型(复习)
内容模型可以为:
1. Any
2. Empty - 无可扩展开的子元素或者节点
3. 简单型(只有文本) - 只有文本节点
4. 复杂型(只有元素) - 只有子元素
5. 混合型 – 既有子元素又有文本节点
忽略属性、注释和PI。注意与后面数据类型的区别。从此处开始一定要区分好内容模型和数据类型,一定不要混,不然后面就乱了。
数据类型
简单类型 – 包含一个简单内容(只有文本)没有任何属性
复杂类型:
l 可以含有any、empty、simple、complex(只有元素)或者一个mixed内容模型
l 带有属性的简单内容被当作复杂类型
l 所有的复杂类型都是用户自定义数据类型
内建数据类型:
由W3C规范预先定义的类型,http://www.w3.org/TR/xmlschema-2/#built-in-datatypes
Ø 原始数据类型
例如:string, date, float, decimal其他
Ø 衍生数据类型
例如interger, nonNegativeInteger.衍生自decimal.
例子
<xs:element name="author" type="xs:string"/>
用户衍生数据类型
由XML Schema设计者自定义的类型
例如:
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title” type="xs:string“/>
<xs:element name=“publisher” type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
定义与声明
定义:用来定义用户衍生类型,例如:
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string"/>
<xs:element name="editor" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
</xs:complexType>
声明:用来声明一个带有名称或者类型的元素或者属性。
<xs:element name="author" type="xs:string"/>
元素声明:
• <xs:element name=“elementName” type=“dataType”>
简单类型
<xs:element name="author" type="xs:string"/>
复杂类型
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title” type="xs:string“/>
<xs:element name=“publisher” type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
属性定义
<xs:attribute name=“attribute_name” type=“datatype” use=“…”>
属性的数据类型总是简单类型
对于use属性的可选值有
> required
> prohibited
> optional
– 默认值是 optional
– Prohibited主要用来创建一个没有具体属性的衍生类型。
简单类型定义
带有简单内容的简单类型
<title> Harry Potter and The Philosopher Stone </title>
<xs:element name=“title” type=“xs:string”>
复杂类型定义
<book>
<title language=“english”> Harry Potter and The Philosopher Stone </title>
</book>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language" type="xs:string use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
带有简单内容的复杂类型(含属性的简单内容)
<title language=“english”> Harry Potter and The Philosopher Stone </title>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language" type="xs:string“ use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
带有复杂内容的复杂类型
是包含有一个或多个子元素的复杂内容模型 。子元素的结构由如下关键字定义:
–sequence
–choice
–all
Sequence - 有序列表
<book>
<title>Professional XML</title>
<publisher> WROX </publisher>
</book>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title” type="xs:string" maxOccurs="unbounded"/>
< xs:element name=“publisher”type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Choice
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="author">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="middlename" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
<xs:sequence>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="firstname" type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
ALL – 无序列表
列表中的每个候选元素成员是1 (maxOccur=1 and minOccurs=1)
列表中的候选可以是0或者1
– 0 意思是 minOccurs=0或 maxOccurs=1
– 1 意思是 minOccurs=1 且maxOccurs=1
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="author" type="xs:string"/>
<xs:element name="editor" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
带有空内容的复杂类型
有两种创建包含empty内容模型的复杂类型:
Verbose – 严格限制为any类型
Compact – 省略定义内容模型的关键字
Verbose
<xs:element name=“br">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType">
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
Compact
<xs:element name=“br”>
<xs:complexType>
</xs:complexType>
</xs:element>
带有混合型内容的复杂类型
<?xml version="1.0"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mixedContent.xsd">
<title>Harry Potter and The Philosopher's Stone</title> written by J.K Rowling
</book>
<xs:element name="book">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="title" type="xs:string" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
属性的使用
带简单型内容元素的属性定义
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language” type="xs:string“ use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
带复杂性内容元素的属性定义
对于此类应用,我们把属性定义在其子元素的后面
<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:attribute name="ID" type="xs:ID"/>
</xs:complexType>
</xs:element>
带有empty内容元素的属性定义
把属性定义在complexType元素内部。
<img src=“whitehouse.jpg”>
<xs:element name="img">
<xs:complexType>
<xs:attribute name="src" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
元素候选
Element元素的出现次数,可以作为其属性出现在声明中,例如:
<xs:element name="title" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="title" type="xs:string" minOccurs=“0” maxOccurs="unbounded"/>