java开发项目中,经常用到xml配置文件,比如web.xml、applicationContext.xml、pom.xml等。在这些文件中都有xmlns、xmlns:xsi和xsi:schemaLocation配置。例如:
web.xml 配置文件
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>
display-name> <context-param> <param-name>contextConfigLocation
param-name> <param-value>classpath:applicationContext.xml
param-value>
context-param> ......
web-app>
applicationContext.xml 配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" default-autowire="byName"> ......
beans>
pom.xml 配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0
modelVersion> <groupId>report
groupId> <artifactId>report-web
artifactId> <packaging>war
packaging> <version>0.0.1-SNAPSHOT
version> <name>report-web
name> ......
project>
a.xml (这个xml文档携带某个表格中的信息)
<table> <tr> <td>Apples
td> <td>Bananas
td>
tr>
table>
b.xml (这个文档携带有关桌子的信息)
<table> <name>African Coffee Table
name> <width>80
width> <length>120
length>
table>
如果这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的
元素,就会发生命名冲突。XML 解析器无法确定如何处理这类冲突。如何解决这类冲突呢?首先是使用前缀来避免命名冲突。a.xml与b.xml文档使用前缀后格式如下:
a.xml
<h:table> <h:tr> <h:td>Apples
h:td> <h:td>Bananas
h:td>
h:tr>
h:table>
b.xml
<f:table> <f:name>African Coffee Table
f:name> <f:width>80
f:width> <f:length>120
f:length>
f:table>
现在,命名冲突不存在了,这是由于两个文档都使用了不同的名称来命名它们的
元素 ( 和
)。通过使用前缀,我们创建了两种不同类型的
元素。
然后我们再使用命名空间来继续改变这两个xml文件:
a.xml
<h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples
h:td> <h:td>Bananas
h:td>
h:tr>
h:table>
b.xml
<f:table xmlns:f="http://www.w3school.com.cn/furniture"> <f:name>African Coffee Table
f:name> <f:width>80
f:width> <f:length>120
f:length>
f:table>
与仅仅使用前缀不同,我们为
标签添加了一个 xmlns 属性,这样就为前缀赋予了一个与某个命名空间相关联的限定名称。
对于以上, 我的理解是这两个不同含义的
,相当于我们在java项目中建立的两个类,一个用来表示表格,另一个用来表示家具;若我们把这两个类都命名成Table(同一包下),那么程序肯定报错(即相当于xml解析器无法解析);在xml中仅仅使用前缀,这两个
变成 和
,相当于我们命名类时,一个命名成HTable,另一个命名成FTable;使用命名空间后,相当于我们建立了两个不同名称的包,一个是f,另一个是h,在这两个不同的包下,我们分别创建了名称都为Table的类。
因此,XML 命名空间属性被放置于元素的开始标签之中,并使用以下的语法:
xmlns:namespace-prefix="namespaceURI"
a.xml
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples
td> <td>Bananas
td>
tr>
table>
b.xml
<table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table
name> <width>80
width> <length>120
length>
table>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
根据上面的理解,我们知道这句代码中,xsi表示前缀,http://www.w3.org/2001/XMLSchema-instance 表示命名空间唯一名称,那这句代码到底表示什么含义呢?为什么要引用这句代码呢? 首先我们要知道xsi是xml shema instance的缩写,翻译后就是xml文档实例。要理解xsi具体含义,我们先理解xml文档验证,在验证xml文档时,一个合法的xml文档,同样遵守文档类型定义(DTD)的语法规则,例如:
note.xml
<note> <to>George
to> <from>John
from> <heading>Reminder
heading> <body>Don't forget the meeting!
body>
note>
在note.xml文件中,DOCTYPE声明是对外部DTD文件的引用,这个DTD文件的内容如下:
note.dtd
]>
dtd文件的作用是定义xml文档的结构,它使用一系列合法的元素来定义文档结构。比如说,我们在note.xml文档中添加一个元素,那么note.xml就无法验证通过,因为note.dtd中没有定义aa元素。其实大家对dtd文件的使用最多的还是在html文件中,我们创建jsp页面时,在jsp页面会出现一行代码(若使用html5,不会出现这行代码):
这句代码就是说明了我们html文档使用了哪个版本的dtd文件。
对于xml文档验证,W3C支持一种基于xml的dtd替代者,它名为xml schema(注意上面我们提到了xml schema instance这个词义)。
xml schema是描述xml文档结构。xml schema 语言也称作xml schema 定义(xml schema definition , xsd)。那么xml schema文件是什么样式的呢?针对note.xml 我们定义一个名称为note.xsd的xml schema文件,具体内容:
note.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/>
xs:sequence>
xs:complexType>
xs:element>
xs:schema>
针对note.xsd中具体代码含有不再具体解释。那么我们要理解note.xsd是xsd的一个具体的实例,也就是说现在我们拥有了一个xml schema 的一个实例即一个xml schema instance,名称叫note.xsd。接下来,在note.xml 文件中对note.xsd的使用如下:
note.xml
<note xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn note.xsd"> <to>George
to> <from>John
from> <heading>Reminder
heading> <body>Don't forget the meeting!
body>
note>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"这句代码再次出现了,现在我们就明白了,这句代码的意思是我们拥有了一个xml schema instance,这实际编程中这句代码是:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd"
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211581.html原文链接:https://javaforall.net
