文章轉載自:https://yq.aliyun.com/articles/40353
相信很多人和我一樣,在編寫Spring或者Maven或者其他需要用到XML文檔的程序時,通常都是將這些XML文檔頭拷貝過來,並沒有理解其中元素(比如xmlns,xmlns:xsi,xsi:schemaLocation)的真正含義,不知道哪些元素是多余的,也不知道為什么要加那些元素。這樣當有時候網上Copy的XML頭有錯的時候自己卻不知道怎么下手。我也是這樣的,於是今天花了點時間好好的理解了一下這些元素及其用法,現整理與此,在此謝謝各位前輩的經驗,如有總結的不對或者不好的地方,歡迎留言提出各位的寶貴意見。
話不多說,先來一段Spring的XML樣本,相信大家都很眼熟:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<
context:component-scan
base-package
=
"xxx.xxx.controller"
/>
<
context:annotation-config
/>
<
mvc:default-servlet-handler
/>
<
mvc:annotation-driven
/>
<
mvc:resources
mapping
=
"/images/**"
location
=
"/images/"
/>
<
bean
id
=
"xxx"
class
=
"xxx.xxx.xxx.Xxx"
>
<
property
name
=
"xxx"
value
=
"xxxx"
/>
</
bean
>
</
beans
>
|
這個文檔中,根元素<beans/>就不用說了,接下來是xmlns。那么什么是xmlns呢?xmlns其實是XML Namespace的縮寫,可譯為“XML命名空間”,但個人覺得,翻譯后的名字反而不好理解,所以我們就叫它為XML Namespace吧。
為什么需要xmlns?
考慮這樣兩個XML文檔:表示HTML表格元素的<table/>:
1
2
3
4
5
6
|
<
table
>
<
tr
>
<
td
>Apples</
td
>
<
td
>Bananas</
td
>
</
tr
>
</
table
>
|
和描述一張桌子的<table/>:
1
2
3
4
5
|
<
table
>
<
name
>African Coffee Table</
name
>
<
width
>80</
width
>
<
length
>120</
length
>
</
table
>
|
假如這兩個 XML 文檔被一起使用,由於兩個文檔都包含帶有不同內容和定義的 <table> 元素,就會發生命名沖突。XML 解析器是無法確定如何處理這類沖突。為了解決上述問題,xmlns就產生了。
如何使用xmlns?
很簡單,使用語法: xmlns:namespace-prefix="namespaceURI"。其中namespace-prefix為自定義前綴,只要在這個XML文檔中保證前綴不重復即可;namespaceURI是這個前綴對應的XML Namespace的定義。例如,
1
|
xmlns:context="http://www.springframework.org/schema/context"
|
這一句定義了一個http://www.springframwork.org/schema/context的Namespace(這和Java類中的包的聲明很相似),並將其和前綴context綁定。所以上面的Spring XML文檔中會有這么一句:
1
|
<
context:component-scan
base-package
=
"xxx.xxx.controller"
/>
|
這里的<component-scan/>元素就來自別名為context的XML Namespace,也就是在http://www.springframework.org/schema/context中定義的。
我們還可以將前綴定義為abc:
1
|
xmlns:abc="namespaceURI"
|
這樣再使用這個namespaceURI中的元素時,需要以abc為前綴,例如:<abc:xxx/>。再拿上面的例子解釋怎么使用xmlns:
1
2
3
4
5
6
7
|
<!-- 這里xmlns:h="url1"表示這個table是用h作為標記,table的寫法在url1中定義 -->
<
h:table
xmlns:h
=
"url1"
>
<
h:tr
>
<
h:td
>Apples</
h:td
>
<
h:td
>Bananas</
h:td
>
</
h:tr
>
</
h:table
>
|
和:
1
2
3
4
5
6
|
<!-- 這里xmlns:f="url2"表示這個table是用f作為標記,table的寫法在url2中定義 -->
<
f:table
xmlns:f
=
"url2"
>
<
f:name
>African Coffee Table</
f:name
>
<
f:width
>80</
f:width
>
<
f:length
>120</
f:length
>
</
f:table
>
|
后者與前者僅僅使用不同前綴,我們為 <table> 標簽添加了一個 xmlns 屬性,這樣就為前綴賦予了一個與某個命名空間相關聯的限定名稱。此時再把它們放在一起,XML解析器就不會報錯了。
注意:當xmlns被定義在元素的開始標簽中(如這里的<f:table/>)時,所有帶有相同前綴的子元素都會與同一個Namespace相關聯(即<f:table/>里面的<f:name/>和<f:width/>也會使用url2定義的寫法)。
xmlns和xmlns:xsi有什么不同?
xmlns表示默認的Namespace。例如Spring XML文檔中的
1
|
xmlns="http://www.springframework.org/schema/beans"
|
這一句表示該文檔默認的XML Namespace為http://www.springframwork.org/schema/beans。對於默認的Namespace中的元素,可以不使用前綴。例如Spring XML文檔中的
1
2
3
|
<
bean
id
=
"xxx"
class
=
"xxx.xxx.xxx.Xxx"
>
<
property
name
=
"xxx"
value
=
"xxxx"
/>
</
bean
>
|
xmlns:xsi表示使用xsi作為前綴的Namespace,當然前綴xsi需要在文檔中聲明。
xsi:schemaLocation有何作用?
xsi:schemaLocation屬性其實是Namespace為http://www.w3.org/2001/XMLSchema-instance里的schemaLocation屬性,正是因為我們一開始聲明了
1
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
這里才寫作xsi:schemaLocation(當然一般都使用這個前綴)。它定義了XML Namespace和對應的XSD(Xml Schema Definition)文檔的位置的關系。它的值由一個或多個URI引用對組成,兩個URI之間以空白符分隔(空格和換行均可)。第一個URI是定義的XML Namespace的值,第二個URI給出Schema文檔的位置,Schema處理器將從這個位置讀取Schema文檔,該文檔的targetNamespace必須與第一個URI相匹配。例如:
1
2
|
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。這里我們可以打開這個Schema的位置,下面是這個文檔的開始部分:
1
2
3
4
5
6
7
8
9
|
<
xsd:schema
xmlns
=
"http://www.springframework.org/schema/context"
xmlns:xsd
=
"http://www.w3.org/2001/XMLSchema"
xmlns:beans
=
"http://www.springframework.org/schema/beans"
xmlns:tool
=
"http://www.springframework.org/schema/tool"
<!-- 這里的targetNamespace和上方xsi:schemaLocation中的第一個URI匹配 -->
targetNamespace="http://www.springframework.org/schema/context"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
|
有了上面的說明后,再去理解開始的Spring XML文檔,一定會有不一樣的感覺!
最后再次感謝各位前輩的寶貴經驗。