lastChild and previousSibling

The lastChild and previousSibling properties of a DOM Document are the inverse of firstChild and nextSibling.

lastChild is a reference to the last child node in a sequence of nodes -- in other words, childNodes[count(childNodes) - 1].

previousSibling is a reference to the node immediately prior to the current node -- for instance, the previousSibling of childNodes[2] is childNodes[1].

The following example reverses the loop from the previous example:

if ($cdCollection->documentElement->hasChildNodes()) {
	$currentNode =& $cdCollection->documentElement->lastChild;
	$i = $cdCollection->documentElement->childCount - 1;
	
	while ($currentNode != null) {
		echo ("The id of child node $i is: " . 
			$currentNode->getAttribute("id") . 
			"<br />");
			
		$currentNode =& $currentNode->previousSibling;
		$i--;		
	}
}


The results are a reverse of the childNodes loop:

The id of child node 2 is: 0003
The id of child node 1 is: 0002
The id of child node 0 is: 0001


Documentation generated by ClassyDoc, using the DOMIT! and SAXY parsers.
Please visit Engage Interactive to download free copies.