包含在XML 元素中的属性,是以子Attribute 节点的形态表示。你无法使用childNodes、firstChild与lastChild 节点属性来存取子Attribute 节点,但你可以使用这些节点属性来存取其它形态的子节点。
包含在XML 元素中的属性,是以子Attribute 节点的形态表示。你无法使用childNodes、firstChild与lastChild 节点属性来存取子Attribute 节点,但你可以使用这些节点属性来存取其它形态的子节点。更确切的说,你只能使用Attribute 节点中的attributes 属性来存取子Attribute 节点。
注意
DOM 不仅使用Attribute 节点来代表属性,也代表一些由名值对(name-value pairs)组成的其它型态的XML 元素-即:
处理指令中的名称与值的结合(例如在XML 宣告中的version="1.0")。
在文件形态宣告、外部实体宣告,或卷标宣告中,关键词SYSTEM 后面接着一个系统literal。
在未解析实体宣告中,关键词NDATA 后面接着一个系统literal。
思考位于列表9-6 中的范例XML 文件。(你可从随书光盘中的 Inventory Attributes.xml 档案中找到列表。)
Inventory Attributes.xml
<?xml version="1.0"?>
<!--File Name:Inventory Attributes.xml -->
<INVENTORY>
<BOOK Binding="mass market paperback" InStock="yes"
Review="***">
<TITLE>The Adventures of Huckleberry Finn</TITLE>
<AUTHOR Born="1835">Mark Twain</AUTHOR>
<PAGES>298</PAGES>
<PRICE>$5.49</PRICE>
</BOOK>
<BOOK Binding="hardcover" InStock="no">
<TITLE>Leaves of Grass</TITLE>
<AUTHOR Born="1819">Walt Whitman</AUTHOR>
<PAGES>462</PAGES>
<PRICE>$7.75</PRICE>
</BOOK>
<BOOK Binding="mass market paperback" InStock="yes "
Review="****">
<TITLE>The Legend of Sleepy Hollow</TITLE>
<AUTHOR>Washington Irving</AUTHOR>
<PAGES>98</PAGES>
<PRICE>$2.95</PRICE>
</BOOK>
</INVENTORY>
文件中的每个BOOK 元素,或多或少都有二至三个属性。下列的script 表达式将取得第一个BOOK 元素的Element 节点:
Document.documentElement.childNodes(0)
(这个范例与本节中下一个范例,均假设文件中包含了Document 节点。)这个Element 节点的attributes 属性提供了一个的NamedNodeMap 集合,此集合为属于第一个BOOK 元素的所有Attribute 节点:
NamedNodeMap =Document.documentElement.childNodes(0).attributes
NamedNodeMap 集合对象,与childNodes 节点属性所提供的NodeList 集合对象稍微有些不同。
表格9-7 列出了NamedNodeMap 对象所提供的属性和一些有用的方法:
NamedNodeMap 属性 描述 范例 Length 此集合所包含的节点个数 AttributeCount=Element.attributes.length; NamedNodeMap 方法 描述 范例 GetNamedItem(att-name) 传回拥有的Attribute节点特定名称 Attribute=Element.attributes.getNamedItem("Binding"); item(0-based-index)(default method) 传回位于index 指定位置的节点,其中0代表第一节点 SecondAttribute=Element.attributes.item (1);
orSecondAttribute=Element.attributes (1); reset ()
nextNode () 将内部指针设成集合中第一个节点位置之前,所以当下个nextNode被呼叫时,将传回第一个节点传回集合中的下一个节点,是籍由内部指针的来标签 Element.attributes.reset ();
Element.attributes.reset ();FirstAttribute
=Element.attributes. nextNode (); 表格9-7 NamedNodeMap 对象所提供的有用属性和方法。而attributes 节点属性提供了一个NamedNodeMap 对象。
你可以使用NamedNodeMap 对象的length 属性与预设的item 方法,在NamedNodeMap 对象集合中切换节点并撷取个别的Attribute 节点。例如,下面的script 程序代码显示范例文件中的第一个BOOK 元素中每一个属性相对应的名称和属性值:
NamedNodeMap =Document.documentElement.childNodes(0).attributes;
for (i=0;i<NamedNodeMap.length;++i)
alert ("node name:" ++NamedNodeMap(i).nodeName +"\n"
+"node value:" ++NamedNodeMap(i).nodeValue);
每对名值对将被显示在「警告」消息框中。下图为执行的结果:

注意,Attribute 节点的nodeName 属性包含了属性名称,而nodeValue 属性则包含了属性值。
注意
Attribute 节点事实上拥有一个包含此属性值的子Text 节点。但因为你可以轻易地从Attribute 节点的nodeValue 属性中得到属性值,所以该子Text 节点并不是必要的。因此在此章中,我们将忽略此形态的Text 节点。
你也可以藉由呼叫NamedNodeMap 对象的getNamedItem 方法来从NamedNodeMap 对象撷取特定的Attribute 节点。例如,下面script 程序代码将显示范例文件中第一个BOOK元素Binding属性的属性值:
NamedNodeMap =Document.documentElement.childNodes(0).attributes;
alert (NamedNodeMap.getNamedItem("Binding ").nodeValue);
下图则为「警告」消息框的结果画面:
