在继续以前你应对如下内容有基本了解:java
HTML / XHTMLui
XML and XML Namespaces
XML 和 XML 名称空间spa
A basic understanding of DTD
对DTD有基本的了解rest
Schema约束orm
XML Schema是以XML语言为基础的,它用于可替代DTD。xml
一份XML schema文件描述了XML文档的结构。element
XML Schema语言也被称为XML Schema Definition (XSD)(XML Schema定义). 开发
1:Schema(*.xsd)文件就是一个xml文件。(DTD不是一个xml文件)文档
2:*.xsd文件,能够更加具体限制数据类型,出现的次数。get
namespace - java包名。用于区分不一样的类。namespace命名空间中,用于区分不一样的元素.
<?xml version="1.0" encoding="UTF-8"?>
<users xmlns:a1="http://a.com" xmlns:a2="http://b.com" xmlns="http://c.com">
<a1:user>
</a1:user>
<a2:dog />
<jack/>
</users>
如下是用schema来约束xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="U002">
<name>李四</name>
<age>23</age>
</user>
</users>
第一步:建立一个xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<schema //schema的根不能修改
xmlns="http://www.w3.org/2001/XMLSchema" - 默认的命名空间,这个命名空间来自于w3,这个命名空间,已经集成到了全部开发环境中
targetNamespace="http://www.example.org/users" - 用户能够修改的命名空间,用于作广播或是引用。
xmlns:tns="http://www.example.org/users"- 和targetNamespace保持一致。
elementFormDefault="qualified" - 彻底限定的名称
>
</schema>
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xx.com"
xmlns:tns="http://xx.com" elementFormDefault="qualified">
<!-- 定义根元素 -->
<element name="users">
<complexType>
<sequence>
<element name="user" maxOccurs="1" minOccurs="1">
<complexType>
<sequence>
<element name="name" maxOccurs="1" type="string"
minOccurs="1" />
<element name="age" type="integer" maxOccurs="1"
minOccurs="1" />
</sequence>
<attribute name="id" use="required" type="ID">
</attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
第二步:在users.xml中引用这个*.xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xx.com" xsi:schemaLocation="http://xx.com users.xsd">
<user id="U002">
<name>李四</name>
<age>23</age>
</user>
</users>
如下是完整的xsd约束:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xx.com"
xmlns:tns="http://xx.com" elementFormDefault="qualified">
<!-- 定义根元素 -->
<element name="users">
<complexType>
<sequence>
<element name="user" maxOccurs="unbounded" minOccurs="1">
<complexType>
<sequence>
<element name="name" maxOccurs="1" minOccurs="1">
<simpleType>
<restriction base="string">
<minLength value="3"></minLength>
<maxLength value="6"></maxLength>
</restriction>
</simpleType>
</element>
<element name="age" maxOccurs="1" minOccurs="1">
<simpleType>
<restriction base="integer">
<minExclusive value="1"></minExclusive>
<maxInclusive value="100"></maxInclusive>
</restriction>
</simpleType>
</element>
</sequence>
<attribute name="id" use="required" type="ID">
</attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
经下是引用这个xsd的CODE:
<?xml version="1.0" encoding="UTF-8"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xx.com" xsi:schemaLocation="http://xx.com users.xsd"> <user id="U002"> <name>李四1</name> <age>23</age> </user> <user id="U001"> <name>李四2</name> <age>23</age> </user> <user id="U004"> <name>李四weew</name> <age>100</age> </user></users>