下面是一个XML文档,该文档表示一个表格信息,为了解决该XML文档与其他XML文档冲突。因此,引入了命名空间:
<h:table xmlns:h="https://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>注意:
(1)上面引入了h命名空间
(2)上面在每个标签前面添加了h:前缀,使用起来非常不便
为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。
请使用下面的语法:
xmlns="namespaceURI"
这个XML文档携带着Spring bean的命名空间信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans">
<bean id="bean5" class="cn.com.spring.Bean5">
<property name="password" value="123"/>
</bean>
</beans>注意:
(1)上面定义了https://www.springframework.org/schema/beans为默认命名空间;因此,标签前面可以不添加前缀
XML命名空间属性被放置于元素的开始标签之中,并使用以下的语法:
xmlns:namespace-prefix="namespaceURI"
当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。
如下:下面是Spring的XML文档,其中定义了一个xmlns:context属性命名空间;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="
https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<!-- 扫描路径 -->
<context:component-scan base-package="com.plate;android.app"/>
<bean id="bean5" class="cn.com.spring.Bean5">
<property name="password" value="123"/>
</bean>
</beans>注意:
(1)用于标记命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。不过,很多公司常常会使命名空间指向实际存在的网页,这个网页包含关于命名空间的信息;
(2)使用命名空间属性可以包含多个不同的命名空间;
该属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。
<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="https://www.sunxin.org/book" (1)
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" (2)
xsi:schemaLocation="
https://www.sunxin.org/book
https://www.sunxin.org/book.xsd"> (3)
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>其中:
(1)声明默认的名称空间( https://www.sunxin.org/book )
(2)声明XML Schema实例名称空间( https://www.w3.org/2001/XMLSchema-instance ),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例名称空间的前缀通常使用xsi。
(3)使用xsi:schemaLocation属性指定;
a、名称空间( https://www.sunxin.org/book )
b、模式位置( https://www.sunxin.org/book.xsd )
注意:在本例中,book.xsd中声明的目标名称空间是:https://www.sunxin.org/book
XML文件对应的book.xsd文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema"
xmlns="https://www.sunxin.org/book"
targetNamespace="https://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引用对之间使用空白符分隔。实例文档使用了多个名称空间,xsi:schemaLocation属性的值包含了两对URI:
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="https://www.sunxin.org/bks"
xmlns:p="https://www.sunxin.org/people"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://www.sunxin.org/bks
https://www.sunxin.org/bks/bks.xsd
https://www.sunxin.org/people
https://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>注意:
(1)XML Schema推荐标准中指出,xsi:schemaLocation属性可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:schemaLocation属性必须出现在它要验证的任何元素和属性之前。
(2)要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:schemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:schemaLocation属性。
该属性用于引用没有目标名称空间的模式文档。与xsi:schemaLocation属性不同的是,该属性的值是单一的值,只是用于指定模式文档的位置。下实例文档中xsi:noNamespaceSchema Location属性的使用:
<?xml version="1.0" encoding="GB2312"?>
<book xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd"
isbn="978-7-121-06812-6">
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>注意:
(1)与xsi:schemaLocation属性一样,xsi:noNamespaceSchemaLocation属性也可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:noNamespaceSchemaLocation属性必须出现在它要验证的任何元素和属性之前。
(2)要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:noNamespaceSchemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:noNamespaceSchemaLocation属性。