xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance(xsi:schemaLocation详解)
http://topic.csdn.net/u/20110117/01/3e3cc322-8f8e-4c2b-8cbb-768bf7319c59.html
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"中xsi的意思是 :
本xml文件中要用到某些來自xsi代表的“http://www.w3.org/2001/XMLSchema-instance”這個命名空間的元素?
比如用來引入無命名空間schema文件的noNamespaceSchemaLocation="XXX";
以及引入自帶命名空間的schema文件的schemaLocation="XXX"這些元素。
這些元素是包含在xsi命名空間中的,所有的xml文件只要引用這些元素 就要引入xsi這個命名空間。 ?
xsi這三個字母不是硬性規定,只是大家都這么用,方便閱讀而已。
?
xsi是http://www.w3.org/2001/XMLSchema-instance的別名。
這樣用于下面元素的時候可以這樣<xsi:element />而不用帶上長長的uri。
有了命名空間以后,在同一級元素就可以使用同一個元素名稱而不會混亂:
<xsi1:element />
<xsi2:element />
?
===============
http://happay99.blog.hexun.com/59496631_d.html
在實例中引用模式文檔
XML Schema提供了兩個在實例文檔中使用的特殊屬性,用于指出模式文檔的位置。這兩個屬性是:xsi:schemaLocation和xsi:noNamespaceSchemaLocation,前者用于聲明了目標名稱空間的模式文檔,后者用于沒有目標名稱空間的模式文檔,它們通常在實例文檔中使用。
4.5.7.1? xsi:schemaLocation屬性
xsi:schemaLocation屬性的值由一個URI引用對組成,兩個URI之間以空白符分隔。第一個URI是名稱空間的名字,第二個URI給出模式文檔的位置,模式處理器將從這個位置讀取模式文檔,該模式文檔的目標名稱空間必須與第一個URI相匹配。我們看例4-28。
例4-28? book6.xml
| <?xml version="1.0" encoding="GB2312"?> <book xmlns="http://www.sunxin.org/book" ??① xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ?② xsi:schemaLocation="http://www.sunxin.org/book http://www.sunxin.org/ book.xsd">??③ <title>《Struts 2深入詳解》</title> <author>孫鑫</author> </book> |
① 聲明默認的名稱空間(http://www.sunxin.org/book)。
② 聲明XML Schema實例名稱空間(http://www.w3.org/2001/XMLSchema-instance),并將xsi前綴與該名稱空間綁定,這樣模式處理器就可以識別xsi:schemaLocation屬性。XML Schema實例名稱空間的前綴通常使用xsi。
③ 使用xsi:schemaLocation屬性指定名稱空間http://www.sunxin.org/book和模式位置http://www.sunxin.org/book.xsd相關。要注意,在這個例子中,book.xsd中聲明的目標名稱空間要求是http://www.sunxin.org/book。
一個可能的模式文檔book.xsd如例4-29所示。
例4-29? book.xsd
| <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sunxin.org/book" targetNamespace="http://www.sunxin.org/book" elementFormDefault="qualified"><xs:element name="book" type="bookType"/> <xs:complexType name="bookType"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> </xs:sequence>?? </xs:complexType> </xs:schema> |
實際上,xsi:schemaLocation屬性的值也可以由多個URI引用對組成,每個URI引用對之間使用空白符分隔。例4-30的實例文檔使用了多個名稱空間,xsi:schemaLocation屬性的值包含了兩對URI。
例4-30? books.xml
| <?xml version="1.0" encoding="GB2312"?> <books xmlns="http://www.sunxin.org/bks" xmlns:p="http://www.sunxin.org/people" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sunxin.org/bks bks.xsd http://www.sunxin.org/people people.xsd"> <book> <title>JSP深入編程</title> <author> <p:name>張三</p:name> <p:title>作家</p:title> </author> </book> <book> <title>XML從入門到精通</title> <author> <p:name>李四</p:name> <p:title>教師</p:title> </author> </book> </books> |
XML Schema推薦標準中指出,xsi:schemaLocation屬性可以在實例中的任何元素上使用,而不一定是根元素,不過,xsi:schemaLocation屬性必須出現在它要驗證的任何元素和屬性之前。
此外,要注意的是,XML Schema推薦標準并沒有要求模式處理器必須要使用xsi:schemaLocation屬性,某些模式處理器可以通過其他的方式來得到模式文檔的位置,而忽略xsi:schemaLocation屬性。
xsi:noNamespaceSchemaLocation屬性
xsi:noNamespaceSchemaLocation屬性用于引用沒有目標名稱空間的模式文檔。與xsi:schemaLocation屬性不同的是,xsi:noNamespaceSchemaLocation屬性的值是單一的值,只是用于指定模式文檔的位置。例4-31顯示了在實例文檔中xsi:noNamespaceSchema Location屬性的使用。
例4-31? book7.xml
| <?xml version="1.0" encoding="GB2312"?> <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="book.xsd" isbn="978-7-121-06812-6" > <title>《Struts 2深入詳解》</title> <author>孫鑫</author> </book> |
與xsi:schemaLocation屬性一樣,xsi:noNamespaceSchemaLocation屬性也可以在實例中的任何元素上使用,而不一定是根元素,不過,xsi:noNamespaceSchemaLocation屬性必須出現在它要驗證的任何元素和屬性之前。
此外,要注意的是,XML Schema推薦標準并沒有要求模式處理器必須要使用xsi:noNamespaceSchemaLocation屬性,某些模式處理器可以通過其他的方式來得到模式文檔的位置,而忽略xsi:noNamespaceSchemaLocation屬性。
?
?
=============
http://xj84.iteye.com/blog/1135958
xmlns與xsi:schemaLocation含義
xmlns 說明參見 http://www.cnblogs.com/jhxk/articles/1619105.html
xsi:schemaLocation說明參見http://www.w3school.com.cn/schema/schema_example.asp
?
xsi:schemaLocation中的模式文檔.xsd文件一般都在本地,比如spring
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans?
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd?
http://www.springframework.org/schema/aop?
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd?
http://www.springframework.org/schema/util?
http://www.springframework.org/schema/util/spring-util-2.5.xsd ">
中的.xsd文件都在spring包內,如下圖:
?spring.schemas中定義了文檔的具體地址:
=============================
http://www.programfan.com/club/showpost.asp?id=24294
schemaLocation?屬性是在?XML?架構實例命名空間?http://www.w3.org/2001/XMLSchema-instance(通常與前綴?xsi?關聯)中定義的,它僅適用于?XML?實例文檔,而不適用于?XML?架構文檔。它區別于在?XML?架構命名空間?http://www.w3.org/2001/XMLSchema?中定義的?schemaLocation?屬性。?
xsi:schemaLocation?屬性提供一種方法來查找在?XML?實例文檔中定義的命名空間的?XML?架構定義。它的值是用空白分隔的統一資源標識符?(URI)?對的列表,其中的每一對?URI?都依次包含一個命名空間以及該命名空間的?XML?架構定義(通常為?.xsd?文件)的位置。?
當將?XML?文檔反序列化為對象時,XmlSerializer?類忽略?xsi:schemaLocation?屬性。但是,在驗證?XML?文檔時,XmlValidatingReader?類可以使用該屬性值來獲取?XML?架構定義。
//JDom里面Namespace是單體模式,報“xsi命名空間已定義”的錯誤就是因為程序里實例化了兩個URL都為xsi而Value不同的Namespace實例
總結
以上是生活随笔為你收集整理的xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance(xsi:schemaLocation详解)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XSD(Schema)教程
- 下一篇: Spring如何加载XSD文件