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>

可以看见table在英文中有两种意思,表格桌子。这时候就会出现两种标准,所以为了让标准统一,让解析的软件(比如浏览器)正确的解析其中的键值对所以我们必须防止命名的冲突。这时候就出现了命令空间来解决该问题。

初步认识命令空间

先来看一个大概。

<!--这个 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>
<!--此 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>

可以看见我们做了我们做了两件事,加了一个前缀和一个xmln属性。通过这样就能很好的防止名称冲突的问题。

Note
xmlns :为xml namespace 的缩写。格式如下

xmlns:namespace-prefix="namespaceURI"

注意他是一个URI,所以作用就是标识什么资源的。但是现在很多厂家给的是一条URL。而这些链接一方面有唯一性防止命名冲突,一方面又是可访问的,其中的信息有助于解析器的解析。

当然我们还两种用法:
一 、同时指定两个命名空间

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture">

<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>

</root>

父标签定义了两个命名空间,在子标签种我们就可以使用不同的命名空间了。
二、默认的命名空间

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

可以看见父标签没有使用xmlns:namespace-prefix="namespaceURI"格式。这就是默认的命名空间。当子标签中没写前缀时,就会使用这个默认命名空间。

案例

这是java 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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.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>
</beans>

首先在beans 这个父标签中我们定义了一个默认命名空间。还有很多别的命名空间xsi,context,mvc,schemaLocation,所以beans的子标签就会出现在各个子标签的命名空间种。如果没写命名空间的如bean 就会在默认命名空间中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值