/** * Perform the query on Document * * @param string $expression CSS selector or XPath query * @param Document $document Document to query * @param string $type The type of $expression * @return NodeList */ public static function execute($expression, Document $document, $type = self::TYPE_XPATH) { // Expression check if ($type === static::TYPE_CSS) { $expression = static::cssToXpath($expression); } $xpath = new DOMXPath($document->getDomDocument()); $xpathNamespaces = $document->getXpathNamespaces(); foreach ($xpathNamespaces as $prefix => $namespaceUri) { $xpath->registerNamespace($prefix, $namespaceUri); } if ($xpathPhpfunctions = $document->getXpathPhpFunctions()) { $xpath->registerNamespace('php', 'http://php.net/xpath'); $xpathPhpfunctions === true ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($xpathPhpfunctions); } $nodeList = $xpath->queryWithErrorException($expression); return new NodeList($nodeList); }
/** * Prepare node list * * @param DOMDocument $document * @param string|array $xpathQuery * @return array * @throws ErrorException If query cannot be executed */ protected function getNodeList($document, $xpathQuery) { $xpath = new DOMXPath($document); foreach ($this->xpathNamespaces as $prefix => $namespaceUri) { $xpath->registerNamespace($prefix, $namespaceUri); } if ($this->xpathPhpFunctions) { $xpath->registerNamespace("php", "http://php.net/xpath"); $this->xpathPhpFunctions === true ? $xpath->registerPHPFunctions() : $xpath->registerPHPFunctions($this->xpathPhpFunctions); } $xpathQuery = (string) $xpathQuery; $nodeList = $xpath->queryWithErrorException($xpathQuery); return $nodeList; }