在繼續以前你應對如下內容有基本瞭解: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>