在您的问题中,它看起来像是一个错字,没有命名函数,ends-with
因此我希望它不起作用:
//tr[/td/a/img[php:function('ends-with',@id,'_imgProductImage')]
^^^^^^^^^
而是使用正确的语法,例如正确的函数名:
//tr[/td/a/img[php:function('ends_with',@id,'_imgProductImage')]
^^^^^^^^^
或者例如像下面的例子:
书.xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
<author>Jane Smith</author>
</book>
<book>
<title>PHP Secrets</title>
<author>Jenny Smythe</author>
</book>
<book>
<title>XML basics</title>
<author>Joe Black</author>
</book>
</books>
PHP:
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");
// Register PHP functions (no restrictions)
$xpath->registerPHPFunctions();
// Call substr function on the book title
$nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]');
echo "Found {$nodes->length} books starting with 'PHP':\n";
foreach ($nodes as $node) {
$title = $node->getElementsByTagName("title")->item(0)->nodeValue;
$author = $node->getElementsByTagName("author")->item(0)->nodeValue;
echo "$title by $author\n";
}
如您所见,此示例注册了所有 PHP 函数,包括现有 substr()
函数。
有关DOMXPath::registerPHPFunctions
更多信息,请参阅,这也是代码示例的来源。
我希望这对您有所帮助,如果您对此仍有疑问,请告诉我。
另见: