A concatenation style sheet is an archive containing the element-related commands in an XML file. Because you have used XML to create your own elements, the browser does not know how to properly display these elements.
In the script program listed in 9-3, the text attribute of each sub-element (TITLE, AUTHOR, BINDING, PAGES, and PRICE) is used as a shortcut to get the element character data. For example, the following program code is used to retrieve character data in the TITLE element:
Title.innerText=Document.doc umentElement. childNodes (0). text;
The text attribute not only provides the text content of the element represented by the current node, but also the text content of any subsequent element. When the element does not have any child element, this attribute can correctly retrieve the text content (such as the element TITLE) in the element ). However, if an element contains more than one child element and character data, the text attribute returns all text, as shown in the following example (in this example, "Moby-Dick Or, the Whale 」).
<TITLE> Moby-Dick
<SUBTITLE> Or, the Whale </SUBTITLE>
</TITLE>
To retrieve only the character data of the TITLE element, you must access the subtext node of the TITLE element.
In table 9-1, the nodeValue attribute of the Element node is null. If an element contains character data, the Text is stored in the sub-Text node, and you can access the character data from the nodeValue attribute of the Text node. For example, as shown in the previous example, if the Element Node of the TITLE Element contains the text "Moby-Dick", the program code below will provide the TITLE character data "Moby-Dick 」, excluding character data of SUBTITLE:
Element. firstChild. nodeValue
(Because the character data of the TITLE element is located before its child element, the first child node represents the character data. Therefore, you can use the firstChild attribute to retrieve the character data .) If the character data of an element is scattered with the child element, annotation, or processing command, each individual character data block is represented as the Child Text node to which the element belongs. For example, in the following example, the ITEM Element has three subnodes in the order that the Text node represents the first character data block, and the Element node represents the SUB-ITEM, another Text node represents the second character data block:
<ITEM>
Character data block 1
<SUB-ITEM> sub-item text </SUB-ITEM>
Character data block 2
</ITEM>
Table 9-5 lists the useful attributes and methods provided by the Text node:
| Text node attributes |
Description |
Example |
| Length |
Number of characters contained in the text on this node |
CharacterCount = Text. length; |
| Text Node Method |
Description |
Example |
| SubstringData (char-offset, num-chars) |
Returns a string containing a specific number of characters from the text of the node. Char-offset is the starting position of the substring, and num-chars is the length of the substring. |
SubString = Text. substringData (2, 3); (returns the second, third, and fourth characters from the content of the Text element) |
Table 9-5 text nodes provide a useful attribute and method. The available attributes include the Common node attributes listed in Table 9-2.