03.02.2014 Views

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

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.

DOM Extension ” 107<br />

R e l a t i v e Addressing<br />

The above examples use what is called absolute addressing, similar in concept <strong>to</strong><br />

absolute filesystem paths. The next example covers use relative addressing.<br />

<br />

Where //body//ul/li selects li nodes, //body//ul/li/.. instead selects ul nodes<br />

by using relative addressing (specifically ..) <strong>to</strong> indicate that the parent node of the<br />

addressed node (li) should be returned rather than the addressed node itself. Compare<br />

this <strong>with</strong> the same sequence used <strong>to</strong> refer <strong>to</strong> the parent direc<strong>to</strong>ry of the current<br />

direc<strong>to</strong>ry on a UNIX filesystem.<br />

Addressing Attributes<br />

Examples thus far have dealt <strong>with</strong> element nodes in particular. XPath also supports<br />

the selection of attribute nodes.<br />

// Returns the id attribute nodes of all ul nodes<br />

$list = $xpath->query(’//ul/@id’);<br />

// Returns all ul nodes that have id attribute nodes<br />

$list = $xpath->query(’//ul/@id/..’);<br />

// Returns all id attribute nodes<br />

$list = $xpath->query(’//@id’);<br />

?><br />

• //ul/@li addresses all id attribute nodes associated <strong>with</strong> ul element nodes<br />

that are descendants of the context node.<br />

• N o t e how the use of relative addressing in the second example applies <strong>to</strong> attributes<br />

nodes just as it would apply <strong>to</strong> child element nodes.

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

Saved successfully!

Ooh no, something went wrong!