例一:重點了解名稱空間的相關概念。
下面的例子是一個XML Schema檔案,名為"note.xsd"
[html] view plaincopy
1. <?xml version="1.0"?>
2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
3. targetNamespace="http://www.w3schools.com"
4. xmlns="http://www.w3schools.com"
5. elementFormDefault="qualified">
6. <xsd:element name="note">
7. <xsd:complexType>
8. <xsd:sequence>
9. <xsd:element name="to" type="xs:string"/>
10. <xsd:element name="from" type="xs:string"/>
11. <xsd:element name="heading" type="xs:string"/>
12. <xsd:element name="body" type="xs:string"/>
13. </xsd:sequence>
14. </xsd:complexType>
15. </xsd:element>
16. </xsd:schema>
下面的XML文檔和上文給出的XML Schema相關聯,名為"note.xml"。并且下文的讨論将圍繞這兩個文檔展開。
[html] view plaincopy
1. <?xml version="1.0"?>
2. <note xmlns="http://www.w3schools.com"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.w3schools.com note.xsd">
5. <to>Tove</to>
6. <from>Jani</from>
7. <heading>Reminder</heading>
8. <body>Don't forget me this weekend!</body>
9. </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等。
對于任何一個XML Schema定義文檔(XSD)都有一個最頂層的schema (XSD)元素。而且該schema (XSD)元素定義必須包含這個名稱空間:http://www.w3.org/2001/XMLSchema。即此名稱空間是由XML模式規範定義的标準名稱空間-所有XML模式元素必須屬于該名稱空間。
此片段: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的開頭将是什麼樣子:
[html] view plaincopy
1. <note xmlns="http://www.w3schools.com"
2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. 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,去除預設名稱空間的聲明,并添加一個複雜類型:
[html] view plaincopy
1. <?xml version="1.0"?>
2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
3. targetNamespace="http://www.w3schools.com"
4. elementFormDefault="qualified">
5. <xsd:element name="note">
6. <xsd:complexType>
7. <xsd:sequence>
8. <xsd:element name="to" type="xs:string"/>
9. <xsd:element name="from" type="xs:string"/>
10. <xsd:element name="heading" type="xs:string"/>
11. <xsd:element name="body" type="xs:string"/>
12. </xsd:sequence>
13. </xsd:complexType>
14. </xsd:element>
15. <xsd:element name="Student" type="stu"/>
16. <xsd:complexType name="stu">
17. <xsd:sequence>
18. <xsd:element name="Name" type="xs:string"/>
19. <xsd:element name="Class" type="xs:string"/>
20. </xsd:sequence>
21. </xsd:complexType>
22. </xsd:schema>
上述代碼中,複雜類型stu是找不到的,因為你定義了一個名稱空間"http://www.w3schools.com",該複雜類型存在于"http://www.w3schools.com"中,是以應該修改代碼如下:
[html] view plaincopy
1. <?xml version="1.0"?>
2. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
3. targetNamespace="http://www.w3schools.com"
4. xmlns:student="http://www.w3schools.com"
5. elementFormDefault="qualified">
6. <xsd:element name="note">
7. <xsd:complexType>
8. <xsd:sequence>
9. <xsd:element name="to" type="xs:string"/>
10. <xsd:element name="from" type="xs:string"/>
11. <xsd:element name="heading" type="xs:string"/>
12. <xsd:element name="body" type="xs:string"/>
13. </xsd:sequence>
14. </xsd:complexType>
15. </xsd:element>
16. <xsd:element name="Student" type="student:stu"/>
17. <xsd:complexType name="stu">
18. <xsd:sequence>
19. <xsd:element name="Name" type="xs:string"/>
20. <xsd:element name="Class" type="xs:string"/>
21. </xsd:sequence>
22. </xsd:complexType>
23. </xsd:schema>
若自身并不使用重用元件,僅供外部使用的話,則隻定義targetNameSpace就可以,不用指定别名。
通過上面的例子,我們可以很深刻的了解targetNameSpace。targetNamespace定義了Schema定義的新元素與屬性的名稱空間。而"http://www.w3.org/2001/XMLSchema"名稱空間則定義了element, attribute, complexType, group,simpleType等元素。
了解了上面的兩個例子,Schema的命名空間的内容應該就明了了。