XML的基本语法(5)

4————XML的命名空间

XML Namespace 用于解决同名标记的冲突问题。

4.1————命名空间的引入

单纯地改变标记命名不能彻底解决。

4.2————命名空间的使用

XML命名空间通过指定一个唯一的统一资源标识符URI,并把元素纳入该标识符之中,保证元素的全局唯一性。

命名空间通过xmlns关键字声明,语法形式:

xmlns:命名空间前缀=“命名空间名”

如下代码:

 1 <?xml version="1.0"?>
 2 <hr:employees xmlns:hr="http://www.demo.org/human_resources"
 3         xmlns:boss="http://www.demo.org/big_boss">
 4         <hr:employee>
 5               <hr:name>jinchen</hr:name>
 6               <hr:birthday>1982-08</hr:birthday

7 <hr:major>soft</major>
 8               <boss:comment>表现优秀,涨薪水10%</boss:comment>
 9               <hr:comment>薪水已调整</hr:comment>  
10         </hr:employee>
11 </hr:employees>

两个<comment>标签便具有唯一性。

4.3————默认命名空间

在上面的代码中,大部分代码属于人力资源的命名空间,此时,可使用默认的命名空间减少工作量。

语法:xmlns=“命名空间”

上面的代码可改为:

<?xml version="1.0"?>
<employees xmlns="http://www.demo.org/human_resources"         xmlns:boss="http://www.demo.org/big_boss">
             <employee>
             <name>jinchen</name>
             <birthday>1982-08</birthday>
             <major>soft</major>
             <boss:comment>表现优秀,涨薪水10%</boss:comment>
             <comment>薪水已调整</comment>  
        </employee>
 </employees>

4.4————命名空间的作用域

命名空间作用于声明它的元素以及该元素的所有子元素,除非被其他命名空间覆盖。

 

5————XML文档的规范级别

格式良好

有效

规范化

 

posted @ 2015-08-30 20:41  realM_J  阅读(84)  评论(0)    收藏  举报