xml命名空间和XSD

来源:互联网 发布:应用商店软件下载 编辑:程序博客网 时间:2024/05/23 17:29

在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头拷贝过来,并没有理解其中元素(比如xmlns,xmlns:xsi,xsi:schemaLocation)的真正含义,不知道哪些元素是多余的,也不知道为什么要加那些元素。这样当有时候网上Copy的XML头有错的时候自己却不知道怎么下手,现整理如下
先贴一段spring 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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:component-scan base-package="xxx.xxx.controller" />    <context:annotation-config />    <mvc:default-servlet-handler />    <mvc:annotation-driven />    <mvc:resources mapping="/images/**" location="/images/" />    <bean id="xxx" class="xxx.xxx.xxx.Xxx">        <property name="xxx" value="xxxx" />    </bean>

释义:xml声明和编码不必多说,先看xmlns,xmlns标识xml命名空间,为什么需要命名空间
在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突。
这个 XML 携带 HTML 表格的信息:

<table><tr><td>Apples</td><td>Bananas</td></tr></table>

这个 XML 文档携带有关桌子的信息(一件家具)

<table><name>African Coffee Table</name><width>80</width><length>120</length></table>


假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 table元素,就会发生命名冲突。
XML 解析器无法确定如何处理这类冲突

1.使用前缀来避免命名冲突

<h:table><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr></h:table><f:table><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length></f:table>


在上面的实例中,不会有冲突,因为两个table元素有不同的名称。

2.XML 命名空间 - xmlns 属性
当在 XML 中使用前缀时,一个所谓的用于前缀的命名空间必须被定义。命名空间是在元素的开始标签的 xmlns 属性中定义的。命名空间声明的语法如下。xmlns:前缀=”URI”。

<root><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><f:table xmlns:f="http://www.w3cschool.cc/furniture"><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length></f:table></root>


3.XML 命名空间 - xmlns 属性,为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。它的语法如下:

<table xmlns="http://www.w3.org/TR/html4/"><tr><td>Apples</td><td>Bananas</td></tr></table>


xsi:schemaLocation有何作用?xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
这里才写作xsi:schemaLocation(当然一般都使用这个前缀)。它定义了XML Namespace和对应的XSD(Xml Schema Definition)文档的位置的关系。它的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)。第一个URI是定义的XML Namespace的值,第二个URI给出Schema文档的位置,Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配
总结:开头的这最外层同样也是一个xml文件的标签,后面那一长串也就是所谓的属性,其中xmlns表示命名空间,xmlns=”http://maven.apache.org/POM/4.0.0” 这表示默认命名空间,而下面xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 这个命名空间里面的元素或者属性就必须要以xsi:这种方式来写,比如schemaLocation就是他的一个属性,所以写成xsi:schemaLocation,而默认命名空间不带类似xsi这种,其实xml标签名称有个专业叫法叫做QName,而如果没有前面的xsi:这种一般叫做NCName。所以你看mvn里面的这种就是默认命名空间下面的元素,最后那一行就表示把定义这个命名空间的schema文件给引用进来,好让eclipse这类型工具能够解析和验证你的xml文件是否符合语法规范。等同于

<import namespace="xxx" schemaLocation="xxx.xsd"/>


1 0