7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
17
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18
* @license http://framework.zend.com/license/new-bsd New BSD License
22
* @see Zend_Dom_Query_Css2Xpath
24
require_once 'Zend/Dom/Query/Css2Xpath.php';
27
* @see Zend_Dom_Query_Result
29
require_once 'Zend/Dom/Query/Result.php';
32
* Query DOM structures based on CSS selectors and/or XPath
36
* @copyright Copyright (C) 2008 - Present, Zend Technologies, Inc.
37
* @license New BSD {@link http://framework.zend.com/license/new-bsd}
42
* @const string Document types
44
const DOC_XML = 'docXml';
45
const DOC_HTML = 'docHtml';
46
const DOC_XHTML = 'docXhtml';
63
* @param null|string $document
66
public function __construct($document = null)
68
if (null !== $document) {
69
$this->setDocument($document);
74
* Set document to query
76
* @param string $document
77
* @return Zend_Dom_Query
79
public function setDocument($document)
81
if ('<?xml' == substr(trim($document), 0, 5)) {
82
return $this->setDocumentXml($document);
84
if (strstr($document, 'DTD XHTML')) {
85
return $this->setDocumentXhtml($document);
87
return $this->setDocumentHtml($document);
91
* Register HTML document
93
* @param string $document
94
* @return Zend_Dom_Query
96
public function setDocumentHtml($document)
98
$this->_document = (string) $document;
99
$this->_docType = self::DOC_HTML;
104
* Register XHTML document
106
* @param string $document
107
* @return Zend_Dom_Query
109
public function setDocumentXhtml($document)
111
$this->_document = (string) $document;
112
$this->_docType = self::DOC_XHTML;
117
* Register XML document
119
* @param string $document
120
* @return Zend_Dom_Query
122
public function setDocumentXml($document)
124
$this->_document = (string) $document;
125
$this->_docType = self::DOC_XML;
130
* Retrieve current document
134
public function getDocument()
136
return $this->_document;
144
public function getDocumentType()
146
return $this->_docType;
150
* Perform a CSS selector query
152
* @param string $query
153
* @return Zend_Dom_Query_Result
155
public function query($query)
157
$xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
158
return $this->queryXpath($xpathQuery, $query);
162
* Perform an XPath query
164
* @param string $xpathQuery
165
* @param string $query CSS selector query
166
* @return Zend_Dom_Query_Result
168
public function queryXpath($xpathQuery, $query = null)
170
if (null === ($document = $this->getDocument())) {
171
require_once 'Zend/Dom/Exception.php';
172
throw new Zend_Dom_Exception('Cannot query; no document registered');
175
$domDoc = new DOMDocument;
176
$type = $this->getDocumentType();
179
$success = @$domDoc->loadXML($document);
182
case self::DOC_XHTML:
184
$success = @$domDoc->loadHTML($document);
189
require_once 'Zend/Dom/Exception.php';
190
throw new Zend_Dom_Exception(sprintf('Error parsing document (type == %s)', $type));
193
$nodeList = $this->_getNodeList($domDoc, $xpathQuery);
194
return new Zend_Dom_Query_Result($query, $xpathQuery, $domDoc, $nodeList);
200
* @param DOMDocument $document
201
* @param string|array $xpathQuery
204
protected function _getNodeList($document, $xpathQuery)
206
$xpath = new DOMXPath($document);
207
$xpathQuery = (string) $xpathQuery;
208
if (preg_match_all('|\[contains\((@[a-z0-9_-]+),\s?\' |i', $xpathQuery, $matches)) {
209
foreach ($matches[1] as $attribute) {
210
$queryString = '//*[' . $attribute . ']';
211
$attributeName = substr($attribute, 1);
212
$nodes = $xpath->query($queryString);
213
foreach ($nodes as $node) {
214
$attr = $node->attributes->getNamedItem($attributeName);
215
$attr->value = ' ' . $attr->value . ' ';
219
return $xpath->query($xpathQuery);