來源:https://blog.csdn.net/wanghuan203/article/details/9204337
XML和Schema具有無關平台,技術廠商,簡單,規范統一等特點,極具開放性,所以使用極為廣泛,而且使用簡單,在XML和Schema和,個人認為比較不好理解的一點是其命名空間問題,在這篇博客里詳細進行理解。
名稱空間是W3C推薦標准提供的一種統一命名XML文檔中的元素和屬性的機制。使用名稱空間可以明確標識和組合XML文檔中來自不同標記詞匯表的元素和屬性,避免了名稱之間的沖突。
使用過DTD的人應該知道,命名沖突是DTD的一大問題,而Schema里引入了命名空間的概念,就很好的解決了這個問題。具體來看:
1、聲明名稱空間
名稱空間聲明的一般形式為:第一部分是一個關鍵字xmlns:,第二部分是名稱空間的前綴,第三部分是一個等號,第四部分是雙引號,將第五部分的名稱空間標識URI包括起來。需要注意的是,名稱空間的前綴不能為xml,因為在XML中這個字符串是保留作特殊用途的。例:
xmlns:tns="http://www.whtest.com/" //其中tns為前綴。
還可以隱式聲明名稱空間,即省略掉冒號和名稱空間前綴。例:
xmlns="http://www.whtest.com/" //注意在一個文檔中只能有一個隱式聲明的命名空間
2、Schema中的命名空間:
(1)Schema中的全局成分
全局成分指的是元素xsd:schema的直接子節點,包括元素聲明、屬性聲明、復雜/簡單類型定義、組定義、屬性組定義。
-
-
<xsd:schema xmlns:xsd=”http: //www.w3.org/XML_Schema”
-
targetNamespace=“http: //www.test.com/ns/ns_test“>
-
// Schema的目標名稱空間用屬性targetNamespace在根元素上定義。
-
//Schema的全局成分被放在名稱空間http://www.test.com/ns/ns_test里。
-
(2)Schema中的非全局成分
有時希望將非全局成分定義在目標空間中去,可使用下面方法。
-
-
<xsd:schema xmlns:xsd=”http: //www.w3.org/XML_Schema”
-
targetNamespace=“http: //www.test.com/ns/ns_test“
-
elementFormDefault=“qualified“>
屬性elementFormDefault的默認值是unqualified,也就是規定了只有全局成分才被定義在目標名稱空間中。將elementFormDefault的值賦為qualified,使得目標名稱空間包含非全局的元素定義。同樣,使屬性attributeFormDefault的值賦為qualified,可使得目標名稱空間包含非全局屬性定義。如下:
-
-
<xsd:schema xmlns:xsd=”http: //www.w3.org/XML_Schema”
-
targetNamespace=“http: //www.test.com/ns/ns_test“
-
attributeFormDefault=“qualified“>
也可以修改屬性form的值,使得某些非全局成分不包含在名稱空間中。如下:
<xsd:element name=”name” type=”xsd:string” form=”unqualified”/>
(3)targetNamespace
xsd文件中定義了一個targetNameSpace后,其內部定義的元素,屬性,類型等都屬於該targetNameSpace,其自身或外部xsd文件使用這些元素,屬性等都必須從定義的targetNameSpace中找。
targetNamespace定義了Schema定義的新元素與屬性的名稱空間。而"http://www.w3.org/2001/XMLSchema"名稱空間則定義了element, attribute, complexType, group, simpleType等元素。
若自身並不使用重用組件,僅供外部使用的話,則只定義targetNameSpace就可以,不用指定別名。
3、XML文檔中命名空間
在XML中,名稱空間的使用涉及范疇的概念,范疇即名稱空間的覆蓋范圍,它指的是哪些元素和屬性在該名稱空間中,哪些不在該名稱空間中。名稱空間既可以限定整個XML文檔,也可以只針對XML文檔中的一部分。
(1).名稱空間限定整個XML文檔
-
-
<member_details xmlns=”http://www.testns.com/ns.xsd”>
-
<name>Tom</name>
-
<age>12</age>
-
<sex>male</sex>
-
</member_details>
(2)名稱空間只針對XML文檔中的一部分
-
-
<member_details>
-
<name xmlns=”http://www.testns.com/ns.xsd”>Tom</name>
-
<age>12</age>
-
<sex>male</sex>
-
</member_details>
(3)嵌套的命名空間
-
-
<member_details xmlns=”http://www.testns.com/ns.xsd”
-
xmlns:newns=”http://www.testns/newns.xsd”>
-
<name>Tom</name>
-
<age>12</age>
-
<newns:sex>male</sex>
-
</member_details>
-
// 此例中,除了元素sex被定義在新的名稱空間中外,其余的元素仍然使用原來的名稱空間。
(4)schemaLocation
schemaLocation 屬性引用具有目標名稱空間的 XML 架構文檔(.xsd)。該xml文件中用到的所有新創的元素、屬性等的.xsd文件都必須在這里聲明。
<xsi:schemaLocation="list of anyURI" >
其中的anyURI是一個架構位置,該架構包含限定的(具有名稱空間的架構)架構構造。每一對中的第一個 URI 引用是名稱空間名稱,第二個則是描述名稱空間的架構的位置。
將具有目標名稱空間的架構文檔與實例文檔相關聯。可以列出多對 URI 引用,每一對都有不同的名稱空間名稱部分。
根據萬維網聯合會 (W3C) XML 架構建議,XML 實例文檔可以同時指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 屬性。此外,還可以多次列出同一個命名空間。
以下示例顯示如何使用 xsi:schemaLocation 屬性為多個 XML 架構文檔提供位置信息。
-
<p:Person
-
xmlns:p="http://contoso.com/People"
-
xmlns:v="http://contoso.com /Vehicles"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
-
"http://contoso.com/People
-
http://contoso.com/schemas/people.xsd
-
http://contoso.com/schemas/Vehicles
-
http://contoso.com/schemas/vehicles.xsd
-
http://contoso.com/schemas/People
-
http://contoso.com/schemas/people.xsd">
-
<name>John</name>
-
<age>28</age>
-
<height>59</height>
-
....
-
</p:Person>
(5)noNamespaceSchemaLocation
noNamespaceSchemaLocation 屬性引用沒有目標名稱空間的 XML 架構文檔。
<xsi:noNamespaceSchemaLocation="anyURI" >
與SchemaLocation相同anyURI是一個架構位置,該架構包含非限定的(沒有名稱空間的架構)架構構造。
不要求 XML 架構有名稱空間。若要為沒有目標名稱空間的 XML 架構指定位置,請使用 noNamespaceSchemaLocation 屬性。此屬性中引用的 XML 架構不能有目標名稱空間。因為此屬性不接受 URL 列表,所以只能指定一個架構位置。
根據萬維網聯合會 (W3C) XML 架構建議,XML 實例文檔可以同時指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 屬性。
以下示例顯示如何對包含非限定元素的實例文檔使用 xsi:noNamespaceSchemaLocation 屬性。
-
-
<book:books xmlns:book="http://www.example.org/02"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:noNamespaceSchemaLocation="02.xsd">
-
<book:book id="1">
-
<book:title>Java in action</book:title>
-
<book:content>Java is good</book:content>
-
<book:author>Bruce</book:author>
-
</book:book>
-
</book:books>
通過上邊的分析,我們可以看到,XML和Schema的命名空間標簽使用格式是相同的(這也是Schema相對與DTD的優勢),但XML和Schema都有各自的獨特的屬性,這也是由他們不同的功能決定的,Schema主要給XML提供服務,所以會規定好targetNameSpace來聲明命名空間的名字,而XML需要使用schema的服務,所以需要SchemaLocation來聲明使用的命名空間。
例一:重點理解名稱空間的相關概念。
下面的例子是一個XML Schema文件,名為"note.xsd"
-
-
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-
targetNamespace="http://www.w3schools.com"
-
xmlns="http://www.w3schools.com"
-
elementFormDefault="qualified">
-
<xsd:element name="note">
-
<xsd:complexType>
-
<xsd:sequence>
-
<xsd:element name="to" type="xs:string"/>
-
<xsd:element name="from" type="xs:string"/>
-
<xsd:element name="heading" type="xs:string"/>
-
<xsd:element name="body" type="xs:string"/>
-
</xsd:sequence>
-
</xsd:complexType>
-
</xsd:element>
-
</xsd:schema>
下面的XML文檔和上文給出的XML Schema相關聯,名為"note.xml"。並且下文的討論將圍繞這兩個文檔展開。
-
-
<note xmlns="http://www.w3schools.com"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://www.w3schools.com note.xsd">
-
<to>Tove</to>
-
<from>Jani</from>
-
<heading>Reminder</heading>
-
<body>Don't forget me this weekend!</body>
-
</note>
此片段:xmlns:xsd=" http://www.w3.org/2001/XMLSchema",表明此schema中使用的元素和數據類型來自於" http://www.w3.org/2001/XMLSchema"名稱空間(namespace)。它同樣指出來自於" http://www.w3.org/2001/XMLSchema"名稱空間的元素和數據類型必須使用帶"xsd: "前綴。作為名稱空間的標識符(在聲明中作為元素或屬性的前綴),你也可以不使用xsd或xsi。這個 xmlns屬性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。
此片段:targetNamespace=" http://www.w3schools.com",表明此schema (note, to, from, heading, body)定義的元素來自於" http://www.w3schools.com"名稱空間。這個targetNamespace屬性表示了該schema所對應的名稱空間的URI。也就是說在引用該Schema的其它文檔(包括自身文檔)中要聲明名稱空間,其URI應該是targetNamespace的屬性值。例如在這里因為要用到note.xsd自己定義的擴展數據類型(note, to, from, heading, body),所以也聲明了名稱空間xmlns=" http://www.w3schools.com"。而且該名稱空間是默認名稱空間(沒有前綴)。targetNamespace屬性為在模式中顯式創建的所有新類型均聲明了XML名稱空間。
我們再來看由該schema規定的XML文檔note.xml的開頭將是什么樣子:
-
<note xmlns="http://www.w3schools.com"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://www.w3schools.com note.xsd">
其中缺省名稱空間聲明xmlns="http://www.w3schools.com"就是和剛剛聲明的XML Schema的名稱空間相結合來規定該XML文檔。(即該文檔用到了此名稱空間中定義的數據) xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 是任何XML實例文檔固有的XML模式實例名稱空間,它由XML模式規范定義。而xsi:schemaLocation="http://www.w3schools.com note.xsd"則規定了該名稱空間所對應的schema的位置,即在相同路徑的note.xsd文件。
例二:重點理解Schema文檔使用自身定義類型
xsd文件中定義了一個targetNameSpace后,其內部定義的元素,屬性,類型等都屬於該targetNameSpace,其自身或外部xsd文件使用這些元素,屬性等都必須從定義的targetNameSpace中找。修改一下note.xsd,去除默認名稱空間的聲明,並添加一個復雜類型:
-
-
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-
targetNamespace="http://www.w3schools.com"
-
elementFormDefault="qualified">
-
<xsd:element name="note">
-
<xsd:complexType>
-
<xsd:sequence>
-
<xsd:element name="to" type="xs:string"/>
-
<xsd:element name="from" type="xs:string"/>
-
<xsd:element name="heading" type="xs:string"/>
-
<xsd:element name="body" type="xs:string"/>
-
</xsd:sequence>
-
</xsd:complexType>
-
</xsd:element>
-
<xsd:element name="Student" type="stu"/>
-
<xsd:complexType name="stu">
-
<xsd:sequence>
-
<xsd:element name="Name" type="xs:string"/>
-
<xsd:element name="Class" type="xs:string"/>
-
</xsd:sequence>
-
</xsd:complexType>
-
</xsd:schema>
上述代碼中,復雜類型stu是找不到的,因為你定義了一個名稱空間"http://www.w3schools.com",該復雜類型存在於"http://www.w3schools.com"中,因此應該修改代碼如下:
-
-
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
-
targetNamespace="http://www.w3schools.com"
-
xmlns:student="http://www.w3schools.com"
-
elementFormDefault="qualified">
-
<xsd:element name="note">
-
<xsd:complexType>
-
<xsd:sequence>
-
<xsd:element name="to" type="xs:string"/>
-
<xsd:element name="from" type="xs:string"/>
-
<xsd:element name="heading" type="xs:string"/>
-
<xsd:element name="body" type="xs:string"/>
-
</xsd:sequence>
-
</xsd:complexType>
-
</xsd:element>
-
<xsd:element name="Student" type="student:stu"/>
-
<xsd:complexType name="stu">
-
<xsd:sequence>
-
<xsd:element name="Name" type="xs:string"/>
-
<xsd:element name="Class" type="xs:string"/>
-
</xsd:sequence>
-
</xsd:complexType>
-
</xsd:schema>
若自身並不使用重用組件,僅供外部使用的話,則只定義targetNameSpace就可以,不用指定別名。
通過上面的例子,我們可以很深刻的理解targetNameSpace。targetNamespace定義了Schema定義的新元素與屬性的名稱空間。而"http://www.w3.org/2001/XMLSchema"名稱空間則定義了element, attribute, complexType, group, simpleType等元素。