02.06.2013 Views

XML Demystified

XML Demystified

XML Demystified

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 MS<strong>XML</strong><br />

The first four lines of the InsertBefore() function are the same as those for the<br />

InsertFirst() and InsertLast() functions.<br />

Line five calls the selectNodes() method of the DOMDocument object. This<br />

method requires one argument containing an XPath expression (see Chapter 5) to<br />

identify the node that will come after the new CD in the <strong>XML</strong> document.<br />

This expression says, Look in the /catalog element for a cd element whose upc<br />

attribute is equal to the UPC passed to the selectNodes() method. There can be<br />

more than one match. Therefore, the selectNodes() method returns a collection that<br />

contains references of matching nodes.<br />

Line six evaluates the value of the length property of the node list returned by the<br />

selectNodes() method. If the length is zero, then the CD entered by the user can’t<br />

be located in the <strong>XML</strong> document and an alert message is displayed; then the function<br />

returns to the statement that called it.<br />

Line seven executes if the selectNodes() method returned a node indicating that<br />

the CD was found in the <strong>XML</strong> document. Line seven calls the insertBefore()<br />

method, which is also called by the InsertFirst() function and InsertLast() function.<br />

The insertBefore() method requires two arguments. The first argument references<br />

the new CD and the second argument references the first CD that will come after<br />

the new CD in the <strong>XML</strong> document. The second argument is the first node the<br />

collection returned by the selectNodes() method.<br />

Line eight is the same as it was in the previous functions.<br />

Here’s the <strong>XML</strong> document after the InsertBefore() function executes:<br />

<br />

<br />

<br />

<br />

U2<br />

How to Dismantle an Atomic Bomb<br />

13.98<br />

Interscope Records<br />

2004-11-23<br />

<br />

<br />

Led Zeppelin<br />

Physical Graffiti<br />

22.99<br />

Atlantic<br />

1994-08-16<br />

<br />

169

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!