Класс DOMXPath

(PHP 5, PHP 7)

Введение

Поддерживает XPath 1.0

Обзор классов

DOMXPath {
/* Свойства */
/* Методы */
public __construct ( DOMDocument $doc )
public mixed evaluate ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
public DOMNodeList query ( string $expression [, DOMNode $contextnode [, bool $registerNodeNS = true ]] )
public bool registerNamespace ( string $prefix , string $namespaceURI )
public void registerPhpFunctions ([ mixed $restrict ] )
}

Свойства

document

Содержание

  • DOMXPath::__construct — Создает новый объект класса DOMXPath
  • DOMXPath::evaluate — Вычисляет переданное XPath выражение и возвращает типизированный результат, если возможно
  • DOMXPath::query — Выполняет заданное XPath выражение
  • DOMXPath::registerNamespace — Ассоциирует пространство имен с объектом DOMXPath
  • DOMXPath::registerPhpFunctions — Регистрация функций PHP как XPath функций

Коментарии

<?php
// to retrieve selected html data, try these DomXPath examples:

$file $DOCUMENT_ROOT"test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

$xpath = new DOMXpath($doc);

// example 1: for everything with an id
//$elements = $xpath->query("//*[@id]");

// example 2: for node data in a selected id
//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");

// example 3: same as above with wildcard
$elements $xpath->query("*/div[@id='yourTagIdHere']");

if (!
is_null($elements)) {
  foreach (
$elements as $element) {
    echo 
"<br/>["$element->nodeName"]";

   
$nodes $element->childNodes;
    foreach (
$nodes as $node) {
      echo 
$node->nodeValue"\n";
    }
  }
}
?>
2008-12-14 11:15:26
http://php5.kiev.ua/manual/ru/class.domxpath.html
Автор:
I just spent far too much time chasing this one....

When running an xpath query on a table be careful about table internal nodes (ie: <tr></tr>, and <td></td>).  If the master <table> tag is missing, then query() (and likely evaluate() also) will return unexpected results.

I had a DOMNode with a structure like this:

<td>
    <table></table>
    <table>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</td>

Upon which I was trying to do a relative query (ie: <?php $xpath_obj->query('my/x/path'$relative_node); ?>).

But because of the lone outer <td></td> tags, the inner tags were being invalidated, while the nodes were still recognized.  Meaning that the following query would work:

<?php $xpath_obj->query('*[2]/*[*[2]]'$relative_node); ?>

But when replacing any of the "*" tokens with the corresponding (and valid) "table", "tr", or "td" tokens the query would inexplicably break.
2011-01-05 12:47:20
http://php5.kiev.ua/manual/ru/class.domxpath.html
Note that evaluate() will use the same encoding as the XML document.

So if you have a UTF-16 XML, you will have to query using UTF-16 strings.

You can use iconv() to convert from your code's encoding to the target encoding for better legibility.
2014-09-03 15:03:48
http://php5.kiev.ua/manual/ru/class.domxpath.html
Using XPath expressions can save a lot of programming
and allow you to home in on only the nodes you want.
Suppose you want to delete all empty <p> tags.
If you create a query using the following XPath expression,
you can find <p> tags that do not have any text
(other than spaces), any attributes,
any children or comments:

$expression = "//p[not(@*) 
   and not(*) 
   and not(./comment())
   and normalize-space(text())='']";
   
This expression will only find para tags that look like:

<p>[any number of spaces]</p>
<p></p>

Imagine the code you would have to add if you used
DOMDocument::getElementsByTagName("p") instead.
2017-05-03 19:46:15
http://php5.kiev.ua/manual/ru/class.domxpath.html
You may not always know at runtime whether your file has
a namespace or not. This can make it difficult to create
XPath queries. Use the seriously underdocumented
"namespaceURI" property of the documentElement of a
DOMDocument to determine if there is a namespace.
Use code such as the following:

$doc = new DOMDocument();
$doc->load($file);
$xpath = new DOMXPath($doc);
$ns = $doc->documentElement->namespaceURI;
if($ns) {
  $xpath->registerNamespace("ns", $ns);
  $nodes = $xpath->query("//ns:em[@class='glossterm']");
} else {
  $nodes = $xpath->query("//em[@class='glossterm']");
}
//look at nodes here
2017-05-03 20:51:04
http://php5.kiev.ua/manual/ru/class.domxpath.html
Автор:
When working with XML (as a strict format) might be very important to give a namespace to XPath object in order to make it work properly.

I was experiencing "query" always returning empty node lists, it could not find anything. Only a broad "//*" was able to show off only the root element.

Then found out that registering the namespace reported in the "xmlns" attribute of the root element in the XPath object, and writing the namespace near the elements name, made it work properly.

So for an XML like this (from a sitemap):

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>http://example.com/index.php</loc>
        <lastmod>2005-01-01</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
</urlset>

I needed the following XPath configuration:

<?php

    $doc 
= new DOMDocument;
   
$doc->load("sitemap.xml");
   
$xpath = new DOMXPath($doc);
   
$xpath->registerNamespace('ns''http://www.sitemaps.org/schemas/sitemap/0.9');
   
$nodes $xpath->query('//ns:urlset/ns:url');

?>

Then again, that "xmlns" could be provided dynamically from the root element attribute of course.
2020-01-08 11:16:27
http://php5.kiev.ua/manual/ru/class.domxpath.html

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