名称空间是为了避免命名混乱
声明默认名称空间
<applicationUsers xmlns="http://wrox.com/namespaces/applications/hr/config">
<user firstName="Joe" lastName="Fawcett" />
<user firstName="Danny" lastName="Ayers" />
<user firstName="Catherine" lastName="Middleton" />
</applicationUsers>
xmlns="http://wrox.com/namespaces/applications/hr/config"唯一标识了该名称空间。
根元素及根元素所有的子元素名称都属于上面的默认名称空间,但是元素的属性不属于该名称空间,根元素下的其他组件(如注释)不属于任何名称空间。
<hr:applicationUsers xmlns:hr="http://wrox.com/namespaces/applications/hr/config">
<hr:user hr:firstName="Joe" hr:lastName="Fawcett" />
<hr:user firstName="Danny" lastName="Ayers" />
<hr:user firstName="Catherine" lastName="Middleton" />
</hr:applicationUsers>
上面通过xmlns:hr=""的形式显示声明了hr这一名称空间,如果要使某元素或属性属于该名称空间,在其前面加上hr即可。
两种名称空间混用
<applicationUsers xmlns="http://wrox.com/namespaces/applications/hr/config"
xmlns:ent="http://wrox.com/namespaces/general/entities">
<ent:user firstName="Joe" lastName="Fawcett" />
<ent:user firstName="Danny" lastName="Ayers" />
<ent:user firstName="Catherine" lastName="Middleton" />
</applicationUsers>