Absolute Addressing
The process of using an XPath expression to obtain a set of nodes to which that expression applies is referred to as addressing. The remainder of the chapter will cover various aspects of addressing and related expression syntax. XPath expressions share several similarities with UNIX filesystem paths, both of which are used to traverse conceptual tree structures. See the example below for specific instances of this. The previous HTML example used to illustrate various concepts of markup languages is reused here to showcase XPath addressing. <?php // Load a markup document $doc = new DOMDocument; $doc->loadHTML(' <html> <body> <ul id="thelist"> <li>Foo</li> <li>Bar</li> </ul> </body> </html> '); // Configure an object to query the document $xpath = new DOMXPath($doc); // Returns a DOMNodeList with only the html node $list = $xpath->query('/html'); // Returns a DOMNodeList with only the body node $list = $xpath->query('/html/body'); // Also returns a DOMNodeList with only the body node $list = $xpath->query('//body'); ?>
The single and double forward slash operators can be used multiple times and in combination with each other as shown below. <?php // Returns all ul nodes that are descendants of the body node $list = $xpath->query('//body//ul'); // Returns all li nodes that are children of the ul nodes $list = $xpath->query('//body//ul/li'); ?>
© DOM Extension — Web Scraping >>> Back to TABLE OF CONTENTS <<< | |
Views: 347 | |
Total comments: 0 | |