(PHP 5)

DOMXPath::registerNamespace Ассоциирует пространство имен с объектом DOMXPath


public bool DOMXPath::registerNamespace ( string $prefix , string $namespaceURI )

Ассоциирует URI пространства имен namespaceURI и префикс prefix с объектом DOMXPath.

Список параметров




URI пространства имен.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.


It is mentioned in a few places on the web, but it wasn't mentioned here. You need to use this function to set up a prefix for the default namespace of a document. 

For instance, if you are trying to parse a Microsoft Spreadsheet XML file, which has the default namespace of "urn:schemas-microsoft-com:office:spreadsheet":

$doc = DOMDocument::load("my_spreadsheet.xml);
$xpath = new DOMXPath($doc);
$query = '/m:Workbook/m:Worksheet[1]/m:Table';
$result = $xpath->query($query, $doc);

You can use anything in place of the 'm', but you have to specify something! Just asking for "/Workbook/Worksheet/Table" doesn't work.
2005-03-31 15:46:30
This is called prefix mapping and it is necessary to use xpath to handle documents which have default namespaces. //root/item will search for items with no namespace, not items with the namespace described as the default in the xmlns declaration.  This problem is maddening as it just looks on the surface like xpath isn't working.
2007-04-15 01:33:50
Ok, here's a function that extends XPath syntax with the following special characters:

~ inserts default namespace prefix if defined

# shorthand for text()

% shorthand for comment()

$ shorthand for node()

?* shorthand for processing-instruction()

?foo shorthand for processing-instruction("foo")

? shorthand for processing-instruction("")

^ escapes following character (with literal or SGML entity as needed)

All of the above except ^ are ignored within quoted strings

function extendXPath($str$defns NULL) {
$quote false;
$map = array(
'~' => isset($defns) ? "$defns:" ''
'#' => 'text()',
'%' => 'comment()',
'$' => 'node()'
$out '';
   for (
$i 0$len strlen($str); $i $len$i++) {
$c $str[$i];
      if (!
$quote && array_key_exists($c$map)) {
$out .= $map[$c];
      } else switch (
$c) {
$out .= htmlspecialchars($str[++$i], ENT_QUOTES);
            if (
$quote) {
$out .= $c;
            } elseif (
$str[$i 1] == '*') {
$out .= 'processing-instruction()';
            } else { 
preg_match('/^\w+/'substr($str$i 1), $matches);
$out .= 'processing-instruction("'.$matches[0].'")';
$i += strlen($matches[0]);
$quote = !$quote;
$out .= $c;
2009-03-31 07:55:58
The following code can work for XML default namespace.
= <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="" xmlns:other="" >

$doc = new DOMDocument;
$xpath DOMXPath($doc);


$xpath_str '//atom:entry/atom:published/text()';

$entries $xpath->evaluate($xpath_str);

$entries->item(0)->nodeValue ."\n";

2009-06-16 03:01:38
A general way to register the root default namespace:

= new DomDocument();
$xpath = new DOMXPath($xml);
$rootNamespace $xml->lookupNamespaceUri($xml->namespaceURI);

And then just query:

<?php $elementList $xpath->query('//x:items/x:name'); ?>
2011-09-20 07:58:22
The documentation is not really detailed.
Probably someone is also interested in the possibility of unregistering the namespace.

Worths to note that the original libxml function "xmlXPathRegisterNs()"

* Does NOT accept NULL or empty string for the argument "$prefix".


* Unregisters the namespace if the "$namespaceURI" argument is NULL.

Here the documentation:
2015-03-10 21:32:46

    Поддержать сайт на родительском проекте КГБ