note 99032 deleted from domxpath.query by danbrown
| From: | danbrown@php.net | Date: | Fri, 23 Jul 2010 14:22:35 +0000 |
| Subject: | note 99032 deleted from domxpath.query by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171075@lists.php.net to get a copy of this message | ||
Note Submitter: chris AT cmbuckley DOT co DOT uk
----
Further to the comments below about documents with default namespaces, the following function tweaks
your XPath expression for you:
<?php
function xpath(DOMNode $node, $path) {
$defaultns = 'defaultns';
$xpath = new DOMXPath($node->ownerDocument);
if (null !== $node->namespaceURI) {
$xpath->registerNamespace($defaultns, $node->namespaceURI);
$path = preg_replace('#(::|/\s*|\A)(?![/@].+?|[a-z\-]+::)#', '$1' .
$defaultns . ':$2', $path);
}
return $xpath->query($path, $node);
}
?>