semi_sleep 发表于 2013-2-7 15:04:59

读XML in a Nutshell (3)

本篇内容包括chapter 4 Namespace。跟.net的namespace类似,XML的namespace也是用来区分同名的element,DTD对namespace的支持是非常有限的,其程度仅仅在于保障使用了namespace的xml能够被验证,在实际应用中,目前觉得namespace主要用于帮助parser识别特定的某些标签。例子如下:
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
 <head><title>Three Namespaces</title></head>
 <body>
  <h1 align="center">An Ellipse and a Rectangle</h1>
  <svg:svg xmlns:svg="http://www.w3.org/2000/svg" width="12cm" height="10cm">
   <svg:ellipse rx="110" ry="130" />
   <svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
  </svg:svg>
 <p xlink:type="simple" xlink:href="ellipses.html">More about ellipses</p>
 <p xlink:type="simple" xlink:href="rectangles.html">More about rectangles</p>
 <hr/>
 <p>Last Modified May 13, 2000</p>
 </body>
</html>
1.namespace的语法,如:xlink:type,xlink被称为prefix,type被称为local part,整个被称为qualified name。

2.namespace的定义,类似于attribute的格式,但namespace所使用的xmlns是保留字,在处理是并不被认为是attribute。使用xmlns="http://www.w3.org/1999/xhtml"定义意味着在没有明确指出的情况下,该element及其所有子element都属于该namespace,值得注意的是,这并不包含attribute,使用xmlns:xlink="http://www.w3.org/1999/xlink"则意味着将该uri绑定到xlink这个prefix。

3.namespace的使用,如上面的例子所示。

4.在DTD中使用namespace。在没有命名冲突的情况下,在DTD中使用<!ATTLIST svg xmlns CDATA #FIXED "http://www.w3.org/2000/">这样的定义,可以为某个element及其子element设置默认的namespace。同时在DTD中使用parameter entity可以使namespace的定义更为灵活,如:
<!ENTITY % dc-prefix "dc">
<!ENTITY % dc-colon ":">
<!ENTITY % dc-title "%dc-prefix;%dc-colon;title">
<!ENTITY % dc-creator "%dc-prefix;%dc-colon;creator">
<!ENTITY % dc-description "%dc-prefix;%dc-colon;description">
<!ENTITY % dc-date "%dc-prefix;%dc-colon;date">
<!ELEMENT %dc-title; (#PCDATA)>
<!ELEMENT %dc-creator; (#PCDATA)>
<!ELEMENT %dc-description; (#PCDATA)>
<!ELEMENT %dc-date; (#PCDATA)>
<!ELEMENT rdf:Description ((%dc-title; | %dc-creator; | %dc-description; | %dc-date; )*)>
页: [1]
查看完整版本: 读XML in a Nutshell (3)