XML DOM » Element » normalize

Syntax:
element.normalize( )

This method normalizes all subtree Text nodes, i.e., it combines two or more adjacent ones into a single one.

In 'normal' form, text nodes can only be separated by markup such as tags, comments, processing instructions, CDATA sections and entity references. This form is useful for operations requiring a particular document structure, and ensures that the DOM view of the document remains the same when saved and reloaded.

Examples

Code:
XML:

<Albums>
   <Album ref="CD142" category="Folk">
      <title>Boil The Breakfast Early</title>
      <artist>The Chieftains</artist>
   </Album>
   <Album ref="CD720" category="Pop">
      <title>Come On Over</title>
      <artist>Shania Twain</artist>
   </Album>
   <Album ref="CD024" category="Country">
      <title>Red Dirt Girl</title>
      <artist>Emmylou Harris</artist>
   </Album>
</Albums>

VBScript:

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("albums.xml")

Set Elem = objXMLDoc.documentElement.childNodes.item(2).firstChild
Set Text = objXMLDoc.createTextNode("Move along, nothing to see here.")
Elem.appendChild(Text)
numChildren = Elem.childNodes.length
document.write ("Number of child nodes: " & numChildren)
Elem.normalize

numChildren = Elem.childNodes.length
document.write ("<br>child nodes after normalization: " & numChildren)
document.write("<br>"
Elem.firstChild.nodeValue)
Output:
Number of child nodes: 2
child nodes after normalization: 1
Red Dirt GirlMove along, nothing to see here.
Explanation:

In this example we load the 'albums.xml' file and append a Text Node to the existing child node of the 'title' element of the third album. (The code displays the number of child nodes to confirm that there are now 2.) Then we call the normalize method and count the number of child nodes again. Finally we display the combined content of the one remaining, normalized child node.

Language(s): VBScript XML