Spring被稱為配置地獄,大量的XML需要配置,各類工程的轉(zhuǎn)移等常常因?yàn)槊臻g出問題。
今天對(duì)XML知識(shí)進(jìn)行整理,當(dāng)然 重點(diǎn)是為了理解命名空間
參考文檔:XML Schema第1部分:結(jié)構(gòu)第二版
1.xml 用途
XML 應(yīng)用于 Web 開發(fā)的許多方面,常用于簡(jiǎn)化數(shù)據(jù)的存儲(chǔ)和共享,指可擴(kuò)展標(biāo)記語言(EXtensible Markup Language),具有自我描述性。 (雖然現(xiàn)在傳輸用json格式更為輕便,但在spring中還是有很多的xml配置)
2.XML的樹結(jié)構(gòu)
XML 文檔形成一種樹結(jié)構(gòu)
XML 文檔必須包含根元素。該元素是所有其他元素的父元素。
XML 文檔中的元素形成了一棵文檔樹。這棵樹從根部開始,并擴(kuò)展到樹的最底端。
所有的元素都可以有子元素:如
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
3.XML的解析
DOM(Document Object Model 文檔對(duì)象模型)定義了訪問和操作文檔的標(biāo)準(zhǔn)方法。
- tips
XML Schema 包含很多,并不僅限于命名空間,還有Xpath和其他規(guī)范
The purpose of *XML Schema: Structures* is to define the nature of XML schemas and their component parts
provide an inventory of XML markup constructs with which to represent schemas,
and define the application of schemas to XML documents.
4.命名空間
4.1為什么需要命名空間
是為了防止沖突,舉例來說,2個(gè)table就會(huì)沖突
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
4.2為了解決沖突問題,就需要加前綴,而xml中前綴的命名空間必須被定義
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
當(dāng)在 XML 中使用前綴時(shí),一個(gè)所謂的用于前綴的命名空間必須被定義。
命名空間是在元素的開始標(biāo)簽的 xmlns 屬性中定義的。xml name space
命名空間聲明的語法如下。xmlns:前綴="URI"
- TIp:命名空間 URI 不會(huì)被解析器用于查找信息。
其目的是賦予命名空間一個(gè)惟一的名稱。不過,很多公司常常會(huì)作為指針來使用命名空間指向?qū)嶋H存在的網(wǎng)頁(yè),這個(gè)網(wǎng)頁(yè)包含關(guān)于命名空間的信息。
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
為了更為清晰,寫在頭文件中
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
命名空間為元素定義默認(rèn)的命名空間可以讓我們省去在所有的子元素中使用前綴的工作
xmlns="namespaceURI"
4.3 xmlns:xsi
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
之前我們已經(jīng)理解了xmlns:xsi,其實(shí)也只是一個(gè)命名空間,它可以為任意值,只是為了方便和規(guī)范這么寫罷了
- xsi:schemaLocation
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
我們來看看官方文檔
All schema processors have appropriate attribute declarations for these attributes built in, see ...
[Attribute Declaration for the 'schemaLocation' attribute (§3.2.7)](https://www.w3.org/TR/xmlschema-1/#xsi.schemaLocation) ...
schemal processor模式處理器,對(duì)于每個(gè)注解有有定義,這里就不是隨意的了
| schemaLocation'屬性的屬性聲明 |
| 屬性 | 值 |
|---|---|
| {名稱} | schemaLocation |
| {target namespace} | http://www.w3.org/2001/XMLSchema-instance |
| {type definition} | 匿名簡(jiǎn)單類型定義,如下所示: |
| 屬性 | 值 |
|---|---|
| {名稱} | ·缺席· |
| {target namespace} | http://www.w3.org/2001/XMLSchema-instance |
| {基本類型定義} | 在內(nèi)置·簡(jiǎn)單的UR-類型定義· |
我們可以看到在實(shí)際使用中,schemaLocation后面的uri和xsd成對(duì)出現(xiàn),前者是之前定義的命名空間,后者是定義的xsd結(jié)構(gòu)定義,如spring的你就可以在spring包內(nèi)找到這份xsd。實(shí)際找到xsd文件如,
spring-beans-4.3.1.RELEASE.jar/jar/org/springframework/beans/factory/xml/spring-beans-4.3.xsd
4.4 什么是xsd文件
- XSD是指XML結(jié)構(gòu)定義 ( XML Schemas Definition ),顧名思義就是規(guī)定這個(gè)xml指定的格式是怎么樣的,是DTD的替代品(并不討論2者的差別,但要清楚一點(diǎn)xsd本身沒有自己獨(dú)特的語法,它和xml是一樣的)
- XML Schema描述了XML文檔的結(jié)構(gòu)。可以用一個(gè)指定的XML Schema來驗(yàn)證某個(gè)XML文檔,以檢查該XML文檔是否符合其要求。文檔設(shè)計(jì)者可以通過XML Schema指定一個(gè)XML文檔所允許的結(jié)構(gòu)和內(nèi)容,并可據(jù)此檢查一個(gè)XML文檔是否是有效的。XML Schema本身是一個(gè)XML文檔,它符合XML語法結(jié)構(gòu)??梢杂猛ㄓ玫腦ML解析器解析它。
所以我們導(dǎo)入xsd后,一個(gè)屬性下有哪些子屬性會(huì)有提示,這都是xsd的功勞, 它規(guī)定了有哪些元素是合法的
xsi:schemaLocation 的值由一個(gè)或多個(gè)URI引用對(duì)組成,兩個(gè)URI之間以空白符分隔(空格和換行均可)。第一個(gè)URI是定義的XML Namespace的值,第二個(gè)URI給出Schema文檔的位置,Schema處理器將從這個(gè)位置讀取Schema文檔,該文檔的targetNamespace必須與第一個(gè)URI相匹配
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
這里表示Namespace為http://www.springframework.org/schema/context的Schema的位置為http://www.springframework.org/schema/context/spring-context.xsd。這里我們可以打開這個(gè)Schema的位置
- 那么實(shí)際上,是如何通過 http://www.springframework.org/schema/context/spring-context.xsd
找到對(duì)應(yīng)的spring-beans-4.3.1.RELEASE.jar/jar/org/springframework/beans/factory/xml/spring-beans-4.3.xsd,我們直接找一個(gè)實(shí)際看一下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--xsd文件本身是xml文件,第一行是xml聲明-->
<xsd:schema xmlns="http://www.springframework.org/schema/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/beans">
<!--
xsd作為xml文件,其根元素是schema
屬性xmlns:xsd="http://www.w3.org/2001/XMLSchema"是引入文檔約束的,表示在當(dāng)前文檔導(dǎo)入"http://www.w3.org/2001/XMLSchema"中所描述的規(guī)則,并且使用里面的元素要添加xsd的前綴(和xmlns:xsd相對(duì)應(yīng),也可以指定其它前綴)
屬性targetNamespace="http://www.springframework.org/schema/beans"表示當(dāng)前文檔定義的規(guī)則處于命名空間"http://www.springfarmework.org/schema/beans"下面,xml文檔如需要導(dǎo)入當(dāng)前文檔的規(guī)則,就可以指定這個(gè)命名空間
屬性xmlns="http://www.springframework.org/schema/beans"表示在當(dāng)前文檔中導(dǎo)入"http://www.springframework.org/schema/beans"命名空間下所描述的規(guī)則(即當(dāng)前文檔本身描述的規(guī)則),并且無需使用前綴,也即默認(rèn)命名空間,這樣,在當(dāng)前文檔就可以直接引用所定義的元素了
-->
</xsd:schema>
xsd已經(jīng)通過了targetNamespace規(guī)定了schema的命名空間的引用,所以哪怕之前說xmlns可以隨便命名,在實(shí)際中也是不行的