原文在:

https://yq.aliyun.com/articles/40353

这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html

先来一段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/>就不用说了,接下来是xmlns。那么什么是xmlns呢?xmlns其实是XML Namespace的缩写,可译为“XML命名空间”,但个人觉得,翻译后的名字反而不好理解,所以我们就叫它为XML Namespace吧。 

为什么需要xmlns,避免重名
如何使用xmlns

xmlns:context="http://www.springframework.org/schema/context"

<context:component-scan base-package="xxx.xxx.controller" />
 xmlns和xmlns:xsi有什么不同?

    xmlns表示默认的Namespace。例如Spring XML文档中的

xmlns="http://www.springframework.org/schema/beans"

这一句表示该文档默认的XML Namespace为http://www.springframwork.org/schema/beans。对于默认的Namespace中的元素,可以不使用前缀。例如Spring XML文档中的

1
2
3
<bean id="xxx" class="xxx.xxx.xxx.Xxx">
  <property name="xxx" value="xxxx"/>
</bean>
 
xsi:schemaLocation有何作用?

xsi:schemaLocation属性其实是Namespace为http://www.w3.org/2001/XMLSchema-instance里的schemaLocation属性,正是因为我们一开始声明了

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
第一个URI是定义的 XML Namespace的值,第二个URI给出Schema文档的位置,Schema处理器将从这个位置读取Schema文档,该文档的targetNamespace必须与第一个URI相匹配。例如:

xsi:schemaLocation="http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd"
http://www.springframework.org/schema/context/spring-context.xsd。这里我们可以打开这个Schema的位置,下面是这个文档的开始部分:

<xsd:schema xmlns="http://www.springframework.org/schema/context"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool" <!-- 这里的targetNamespace和上方xsi:schemaLocation中的第一个URI匹配 -->
targetNamespace="http://www.springframework.org/schema/context"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

Done!

【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation的更多相关文章

  1. 操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP)

    原文:操作XML文档遇到的XMLNS问题及解决方法 (C# 和 PHP) 不管是用 PHP 还是 C#, 在操作 XML 的时候我们除了一个节点一个节点去取值之外, 还有一个非常方便的表达式, 就是 ...

  2. XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation

    文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...

  3. 关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

    https://yq.aliyun.com/articles/40353 ************************************* 摘要: 相信很多人和我一样,在编写Spring或者 ...

  4. 关于"XML 文档(2, 2)中有错误:不应有 <xml xmlns=''>"错误

    XML文件名 <?xml version="1.0" encoding="utf-8"?> <Config xmlns:xsi="h ...

  5. [XML] C# XmlHelper操作Xml文档的帮助类 (转载)

    点击下载 XmlHelper.rar 主要功能如下所示 /// <summary> /// 类说明:XmlHelper /// 编 码 人:苏飞 /// 联系方式:361983679 // ...

  6. (转载)将一段符合XML格式规范字符串插入已有XML文档当中

    想我们已经存在一个XML文档,结构如下:   < xmlversion="1.0"encoding="utf-8">< employees&g ...

  7. Android XML文档解析(一)——SAX解析

    ---------------------------------------------------------------------------------------------------- ...

  8. C# 使用XmlDocument类对XML文档进行操作

    原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html 转载请注明出处 W3C制定了XML DOM标准.很多编程语言中多提供 ...

  9. dom4j解析xml文档全面介绍

    一.dom4j介绍 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点, ...

随机推荐

  1. Linux查看日志命令

    tail -f /var/log/apport.log more /var/log/xorg.0.log cat /var/log/mysql.err less /var/log/messages g ...

  2. Codeforces Round #250 (Div. 2)

    感觉不会再爱了,呜呜! A题原来HACK这么多!很多人跟我一样掉坑了! If there is some choice whose description at least twice shorter ...

  3. Css选择器的优先级

    a = 行内样式style. b = ID选择器的数量. c = 类.伪类和属性选择器的数量. d = 类型选择器和伪元素选择器的数量. 选择器 等级(a,b,c,d) style=”” 1,0,0, ...

  4. CSLight研究院之学习笔记结合NGUI(一)

    原地址:http://www.xuanyusong.com/archives/3088 这两天一直在研究CSLight,目前Unity热更新的方式有两种,一种是ulua这个网上的例子已经很多了.还有一 ...

  5. 容器字段FieldContainer

    //Ext.form.FieldContainer扩展自Ext.container.Container.当需要把多个字段或组件作为一个表单项展示的时候就需要此组件          Ext.Quick ...

  6. [排序] 快排 && 冒泡(自己写)

    #include <iostream> using namespace std; /* 快速排序 通过一趟排序,以轴点为界 分割为两部分:左部分 <= 轴点 <= 右部分 再分 ...

  7. ***codeigniter 2.2 affected_rows()返回值不准确

    http://blog.icodeu.com/?p=596 问题描述今天在完成一个项目调用想要检验一下计划插入的数据是否都正常插入了.调用insert_batch()方法插入一百多条数据的时候发现af ...

  8. ElasticSearch使用IK中文分词---安装步骤记录

    提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...

  9. 项目上线与LOG记录

    如果项目上过线的话,那你一定知道Log是多么重要. 为什么说Log重要呢?因为上线项目不允许你调试,你只能通过Log来分析问题.这时打一手好Log的重要性绝不亚于写一手好代码.项目出问题时,你要能拿出 ...

  10. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...