ITeye博客

XML命名空间与验证小结

samjavaeye · 2013-04-07 15:00 · Java · 编程语言 · Markdown

命名空间主要是用来避免元素命名冲突,详细解释可以参考《XML命名空间》。

验证的目的是确保XML文档按照设计者规定的格式编写,减少解析时出错的可能性。例如规定某元素必须具有哪些属性等等。详细描述可以参考《XML Schema 简介》

 

下面看一个例子。

 

<?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:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 

  • 第2行定义的是一个缺省的命名空间,也就是说XML文档中的元素如果没有前缀,都属于这个命名空间。第8、9两行跟此命名空间对应,第8行是命名空间的名称,第9行是该命名空间的Schema(XSD验证文件)的URL。
  • 第3行表示该XML文档遵循XMLSchema规范。
  • 第5行定义了前缀为context的、名称为“http://www.springframework.org/schema/context”的命名空间;第10、11行为该命名空间的名称与对应的Schema URL。
  • schemaLocation的定义总是成对出现,例如12和13、14和15,中间用空格隔开。另外后面一行对应的xsd文件本身也是一个XML文档,并且该xsd文件的命名空间(即xmlns属性)必须跟前一行的命名空间名称一致。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。