版权声明:程序猴jwang版权所有 https://blog.csdn.net/qq_21046965/article/details/86748821
前言
本章学习XML Schema的基本内容
方法
1.概念
首先说明一下,它和DTD文件一样都是约束XML语法的文件,那么为什么要引入XML Schema呢?
DTD文件的劣势:
XML Schema的优势:
- Schema基于XML语法
- Schema可以用能处理XML文档的工具处理
- Schema大大扩充了数据类型,可以自定义数据类型
- Schema支持元素的继承—Object-Oriented’ish
- Schema支持属性组
但是,一些配置文件依旧采取DTD的约束,如hibernate配置文件和MyBatis配置文件等。
2.使用XML Schema代替DTD
我们依然使用之前的XML例子:
<?xml version="1.0" encoding="UTF-8"?>
<note type="local">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
编写XML Schema:
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns:tns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<element name="note">
<complexType>
<sequence>
<element name="to" type="string"/>
<element name="from" type="string"/>
<element name="heading" type="string"/>
<element name="body" type="string"/>
</sequence>
<attribute name="type" type="string"/>
</complexType>
</element>
</schema>
使用之前的XML引入我们编写的XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn hello.xsd" type="local">
<to>1</to>
<from>1</from>
<heading>1</heading>
<body>1</body>
</note>
3.使用XMLSpy对其进行验证
经试验,完全验证通过
更多使用XML Schema的相关细节请访问:http://www.w3school.com.cn/schema/index.asp