XML SchemaIntroduction
XML Schema (也称 for XSD, XML Schema Definition) is a用于定义XMLdocumentationstructure and dataclass型 language, 它 is DTD 替代品, providing了更强 big , 更flexible verificationmechanism.
XML Schema 特点
- 基于XML语法, 易于understanding and writing
- support丰富 dataclass型, including in 置dataclass型 and 自定义dataclass型
- supportnamespace, 便于module化 and 重用
- support元素 and property complex 约束
- supportinheritance and scale, 便于构建 complex documentationstructure
- support国际化 and 本地化
XML Schema and DTD for 比
| features | DTD | XML Schema |
|---|---|---|
| 语法 | 非XML语法 | XML语法 |
| dataclass型 | 仅support#PCDATA | support丰富 dataclass型 |
| namespace | support has 限 | 完全support |
| scale性 | 较差 | 良 good , supportinheritance and scale |
| 约束capacity | has 限 | 强 big , support complex 约束 |
| toolsupport | 广泛support | 现代tool广泛support |
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books" type="BooksType"/>
<xs:complexType name="BooksType">
<xs:sequence>
<xs:element name="book" type="BookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
XML Schema basicstructure
一个XML Schemadocumentation通常package含以 under basicstructure:
XML Schema 根元素
XML Schemadocumentation 根元素 is <xs:schema>, 它package含了所 has Schema定义.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>
XML Schema namespace
XML Schemausingspecific namespace来标识Schema元素 and property. 默认circumstances under , XML Schemausing namespace is http://www.w3.org/2001/XMLSchema, 通常using before 缀xs or xsd来引用.
XML Schema 主要component
- 元素定义: using
<xs:element>元素定义XMLdocumentationin允许using 元素 - complex class型定义: using
<xs:complexType>元素定义package含子元素 and property complex dataclass型 - simple class型定义: using
<xs:simpleType>元素定义只package含文本 in 容 dataclass型 - property定义: using
<xs:attribute>元素定义元素可以package含 property - 组定义: using
<xs:group>元素定义 reusable 元素组 - property组定义: using
<xs:attributeGroup>元素定义 reusable property组
XML Schema dataclass型
XML Schemaproviding了丰富 dataclass型support, including in 置dataclass型 and 自定义dataclass型.
in 置dataclass型
XML Schema in 置dataclass型可以分 for 以 under 几class:
1. stringclass型
xs:string: 任意字符序列xs:normalizedString: 不package含换行, 制表符 and 回车 stringxs:token: 不package含 before 导, 尾随空格 and 连续空格 stringxs:language: 符合RFC 3066 languagecode
2. 数值class型
xs:integer: 整数xs:decimal: 十进制数xs:float: 单精度浮点数xs:double: 双精度浮点数xs:positiveInteger: 正整数xs:negativeInteger: 负整数xs:nonPositiveInteger: 非正整数xs:nonNegativeInteger: 非负整数
3. 日期 and 时间class型
xs:date: 日期 (YYYY-MM-DD)xs:time: 时间 (HH:MM:SS)xs:dateTime: 日期 and 时间 (YYYY-MM-DDTHH:MM:SS)xs:gYear: 年份 (YYYY)xs:gMonth: 月份 (--MM)xs:gDay: 日期 (---DD)
4. booleanclass型
xs:boolean: boolean值, 取值 for true or false
5. 二进制class型
xs:base64Binary: Base64编码 二进制dataxs:hexBinary: 十六进制编码 二进制data
6. otherclass型
xs:anyURI: 统一resource标识符xs:QName: 限定名称 (带namespace before 缀 名称)xs:NOTATION: 表示XML NOTATIONclass型
自定义dataclass型
除了 in 置dataclass型 out , XML Schema还允许through限制 in 置dataclass型 or scale现 has dataclass型来creation自定义dataclass型.
<!-- 限制 in 置dataclass型 -->
<xs:simpleType name="ISBNType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{3}-\d-\d{5}-\d{3}-\d"/>
</xs:restriction>
</xs:simpleType>
<!-- scale in 置dataclass型 -->
<xs:complexType name="ExtendedStringType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language" type="xs:language"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
XML Schema 元素定义
in XML Schemain, 元素定义用于指定XMLdocumentationin允许using 元素, 元素 in 容class型以及元素 出现次数.
元素定义 语法
<xs:element name="元素名" type="dataclass型" [minOccurs="最 small 值"] [maxOccurs="最 big 值"] />
simple 元素
simple 元素 is 指只package含文本 in 容, 不package含子元素 or property 元素.
<xs:element name="title" type="xs:string"/> <xs:element name="year" type="xs:gYear"/> <xs:element name="price" type="xs:decimal"/>
complex 元素
complex 元素 is 指package含子元素 or property 元素.
1. package含子元素 complex 元素
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
2. package含property complex 元素
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="category" type="xs:string"/>
</xs:complexType>
</xs:element>
3. package含 simple in 容 and property complex 元素
<xs:element name="description">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="language" type="xs:language" default="zh-CN"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
元素 出现次数
in XML Schemain, 可以usingminOccurs and maxOccursproperty来指定元素 出现次数.
| property | 默认值 | 说明 |
|---|---|---|
minOccurs |
1 | 元素出现 最 small 次数 |
maxOccurs |
1 | 元素出现 最 big 次数, 值 for "unbounded"表示无限制 |
<!-- 必须出现且仅出现一次 --> <xs:element name="title" type="xs:string"/> <!-- 可选, 最 many 出现一次 --> <xs:element name="subtitle" type="xs:string" minOccurs="0"/> <!-- 必须出现, 可出现任意次数 --> <xs:element name="author" type="xs:string" minOccurs="1" maxOccurs="unbounded"/> <!-- 可选, 可出现任意次数 --> <xs:element name="keyword" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
XML Schema property定义
in XML Schemain, property定义用于指定元素可以package含 property, property dataclass型以及property 默认值.
property定义 语法
<xs:attribute name="property名" type="dataclass型" [use="using规则"] [default="默认值"] [fixed="固定值"] />
property using规则
in XML Schemain, 可以usinguseproperty来指定property using规则:
| using规则 | 说明 |
|---|---|
optional |
property is 可选 (默认值) |
required |
property is 必需 |
prohibited |
property is 禁止using |
<!-- 必需property --> <xs:attribute name="id" type="xs:string" use="required"/> <!-- 可选property --> <xs:attribute name="category" type="xs:string" use="optional"/> <!-- 带 has 默认值 property --> <xs:attribute name="language" type="xs:language" default="zh-CN"/> <!-- 带 has 固定值 property --> <xs:attribute name="version" type="xs:string" fixed="1.0"/>
XML Schema namespace
XML Schema广泛usingnamespace来标识Schema元素 and property, 以及引用otherSchemadocumentation.
XML Schema 目标namespace
目标namespace is 指当 before Schemadocumentation所定义 元素 and property所属 namespace. 可以throughtargetNamespaceproperty来指定.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/books"
xmlns="http://example.com/books"
elementFormDefault="qualified">
<xs:element name="books" type="BooksType"/>
<xs:complexType name="BooksType">
<xs:sequence>
<xs:element name="book" type="BookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
引用带 has namespace XML Schema
当XML Schemadocumentationusing了目标namespace时, in XMLdocumentationin引用该Schema需要usingnamespace.
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://example.com/books"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/books books.xsd">
<book id="B001">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
</books>
实践case: creation并usingXML Schema
casedescribes
creation一个XML Schemafile, 用于定义graph书information structure, 然 after using该SchemaverificationXMLdocumentation.
implementation步骤
- creation一个名 for
books.xsdXML Schemafile - in Schemafilein定义graph书information structure
- creation一个名 for
books.xmlXMLfile - in XMLfilein引用Schema
- usingxmllinttoolverificationXMLdocumentation
最终code
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/books"
xmlns="http://example.com/books"
elementFormDefault="qualified">
<!-- 定义graph书class型 -->
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="year" type="xs:gYear"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="description" type="xs:string" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="category" type="xs:string" default="general"/>
</xs:complexType>
<!-- 定义graph书collectionclass型 -->
<xs:complexType name="BooksType">
<xs:sequence>
<xs:element name="book" type="BookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<!-- 根元素定义 -->
<xs:element name="books" type="BooksType"/>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://example.com/books"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/books books.xsd">
<book id="B001" category="计算机">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
<description>一本全面介绍XMLBasics tutorial</description>
</book>
<book id="B002">
<title>XMLadvancedapplication</title>
<author>李四</author>
<year>2025</year>
<price>129.00</price>
</book>
</books>
verification过程
usingxmllintverificationXMLdocumentation:
# usingSchemaverificationXMLdocumentation xmllint --noout --schema books.xsd books.xml # such as果没 has 输出, 说明documentation is has 效
互动练习
练习1: 识别XML Schemaerror
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students" type="StudentsType"/>
<xs:complexType name="StudentsType">
<xs:sequence>
<xs:element name="student" type="StudentType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="StudentType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer" minOccurs="0" maxOccurs="1"/>
<xs:element name="gender" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required">
</xs:complexType>
</xs:schema>
XML Schema存 in 以 under error:
- attribute元素缺 few 结束tag
/>, 应该 is<xs:attribute name="id" type="xs:string" use="required"/>
练习2: creationXML Schema and XMLdocumentation
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/students"
xmlns="http://example.com/students"
elementFormDefault="qualified">
<xs:complexType name="StudentType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="15"/>
<xs:maxInclusive value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="gender" type="xs:string">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="男"/>
<xs:enumeration value="女"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="major" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="StudentsType">
<xs:sequence>
<xs:element name="student" type="StudentType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:element name="students" type="StudentsType"/>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<students xmlns="http://example.com/students"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/students students.xsd">
<student id="S001">
<name>张三</name>
<age>20</age>
<gender>男</gender>
<major>计算机科学</major>
</student>
<student id="S002">
<name>李四</name>
<age>21</age>
<gender>女</gender>
<major>软件工程</major>
</student>
</students>