Xml命名空間淺析

來源:互聯網
上載者:User

標籤:blog   http   java   使用   strong   檔案   

XML執行個體

在介紹xml命名空間之前,我們先來看段xml代碼:

<?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:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">          <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">    <property name="scopes">      <map>        <entry>          <bean class="org.springframework.context.support.SimpleThreadScope"/>        </entry>        </map>    </property>  </bean>    <bean id="bar" class="x.y.Bar" scope="thread">    <property name="name" value="Rick"/>    <aop:scoped-proxy/>  </bean>  </beans>

這段代碼摘自spring架構的某段配置代碼。

稍微分析一下:該段xml代碼中的諸多元素沒有使用任何首碼,因為它們的預設命名空間是 “http://www.springframework.org/schema/beans”, 該命名空間所對應的schema的地址:http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 

這段代碼還有另外一個以aop為首碼的命名空間"http://www.springframework.org/schema/aop", 該命名空間對應的schema地址:http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

<aop:scoped-proxy/> 這段代碼就使用了以aop為首碼的scoped-proxy元素。

其他元素包括beans,bean,property,map等元素都是通過schema來定義的,下面我們就簡單看下schema的知識。

XML schema介紹

XML Schema 是基於 XML 的 DTD 替代者。
XML Schema 描述 XML 文檔的結構。
XML Schema 語言也稱作 XML Schema 定義(XML Schema Definition,XSD)。

  上面文字摘自W3C中對Schema的介紹。 沒錯,XML Schema就是用來描述XML文檔結構的。

 

下面我們就來簡單寫個schema檔案:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"    targetNamespace="http://www.gogogo.com"    xmlns="http://www.gogogo.com"    elementFormDefault="qualified">          <xs:element name="name" type="xs:string"/>          <xs:element name="Employee">    <xs:complexType>      <xs:sequence>        <xs:element name="id" type="xs:int"/>        <xs:element ref="name"/>        <xs:element name="age" type="xs:int"/>        <xs:element name="birth" type="xs:date"/>      </xs:sequence>    </xs:complexType>  </xs:element>  </xs:schema>

這個簡單的schema表示的意思就是定義一個名字為Employee的複雜類型。 該元素分別擁有順序為id,name,age,birth這4個元素子項目。

XML 命名空間介紹

XML 命名空間提供避免元素命名衝突的方法。

  上面文字摘自W3C中對XML 命名空間的介紹。

 

怎麼理解呢。其實這個命名空間跟.Net中的命名空間或Java中的package的概念是一樣的,就是用來解決一些命名衝突的。

舉個例子:

比如在A.xsd和B.xsd這2個Schema檔案中都定義了Employee這個類型的元素。 那麼如何區別這2個不用類型相同名字的Employee元素?  答案就是使用命名空間進行區分。

 

下面我們來分析一下本文定義的schema中的代碼:

下面我們通過另外一種方式重新寫下這段schema代碼:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"    targetNamespace="http://www.gogogo.com"    xmlns:my="http://www.gogogo.com"    elementFormDefault="qualified">          <xs:element name="name" type="xs:string"/>          <xs:element name="Employee">    <xs:complexType>      <xs:sequence>        <xs:element name="id" type="xs:int"/>        <xs:element ref="my:name"/>        <xs:element name="age" type="xs:int"/>        <xs:element name="birth" type="xs:date"/>      </xs:sequence>    </xs:complexType>  </xs:element>  </xs:schema>

代碼沒什麼變化,只是不使用預設的命名空間了。將該schema對外的命名空間地址用在my首碼下。這樣ref name的時候就必須使用my首碼了,因為已經沒有了預設的命名空間,schema不知道怎麼去找 "name" 這個元素。

 

看了2段代碼,總結一下schema中命名空間的使用:

            xml:你的首碼="你的命名空間地址"

examples:

            xml:my="http://www.my.org",   xml:omg="http://www.omg.org", xml:java="http://www.java.org"  ........

參考資料

http://www.w3school.com.cn/schema/index.asp

http://www.w3school.com.cn/xml/xml_namespaces.asp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.