XPath and DOMXPath

Somewhat similar to the way that regular expressions allow instances of character patterns to be found within strings, XPath allows instances of node patterns to be found within XML-compatible documents. Both technologies accomplish their purpose by providing a syntax comprised of meta-characters to express these patterns in a concise and succinct way. With the DOM extension, support for version 1.0 of the XPath standard is implemented as the DOMXPath class.

The DOMXPath constructor has a single required parameter: an existing DOMDocument instance on which queries will be performed. DOMXPath has two other relevant methods: evaluate and query. Both accept a string containing an XPath expression with which to query the document as their first parameter.

Optionally, a DOMNode instance associated with the document may be passed in as the second parameter ($contextNode) for either method. When specified, that node will become the context node and query results will be restricted to that node and its descendants. Otherwise, the root element of the document is assumed to be the context node.

The difference between evaluate and query is that the latter will always return an instance of DOMNodeList whereas the former will attempt to return an appropriately typed result if only one result is found.


© DOM Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 320 | Rating: 0.0/0
Total comments: 0
avatar