如何在 XPath 中注册 PHP 函数? [英] How to register PHP function in XPath?
问题描述
如何在XPATH
中注册
PHP
函数?因为 XPATH
不允许我使用 ends-with()
How can I register
PHP
function in XPATH
? Because XPATH
does not allows me to use ends-with()
这是一位成员提供的一种解决方案,但它不适用.
Here is one solutions given by one member but it does not works with.
他使用的代码是:
$xpath = new DOMXPath($document);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions("ends_with");
$nodes = $x->query("//tr[/td/a/img[php:function('ends-with',@id,'_imgProductImage')]"
function ends_with($node, $value){
return substr($node[0]->nodeValue,-strlen($value))==$value;
}
我使用的是 PHP 5.3.9.
I am using PHP 5.3.9.
推荐答案
在您的问题中,它看起来像是一个错字,没有名为 ends-with
的函数,因此我希望它不起作用:
In your question it looks like a typo, there is no function named ends-with
therefore I would expect it not to work:
//tr[/td/a/img[php:function('ends-with',@id,'_imgProductImage')]
^^^^^^^^^
改为使用正确的语法,例如正确的函数名:
Instead use the right syntax, e.g. the correct function name:
//tr[/td/a/img[php:function('ends_with',@id,'_imgProductImage')]
^^^^^^^^^
或者例如像下面的例子:
Or for example like with the following example:
book.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()
函数.
As you can see, this example registers all PHP functions including the existing substr()
function.
查看 DOMXPath::registerPHPFunctions
了解更多信息,这也是代码示例的来源.
See DOMXPath::registerPHPFunctions
for more information, that is also where the code example has been taken from.
希望对您有所帮助,如果您对此仍有疑问,请告诉我.
I hope this is helpful, let me know if you still have a question about this.
另见:
- 如何在 php 中使用 preg 添加 html 属性(2010 年 8 月)
- 在 PHP XPath 中使用正则表达式->评估(2011 年 11 月)
- 在 Xpath (PHP) 中获取以大写开头的标签(2012 年 7 月);在特定的这个答案中.
- 从一堆 xml 文件中特定正则表达式模式的搜索结果中获取 xpath(2013 年 3 月);在特定的这个答案中.
- How to use preg in php to add html properties (Aug 2010)
- Using Regex in PHP XPath->evaluate (Nov 2011)
- Get tags that start with uppercase in Xpath (PHP) (Jul 2012); in specific this answer.
- Get xpath from search result of a specific regex pattern in a bunch of xml files (Mar 2013); in specific this answer.
这篇关于如何在 XPath 中注册 PHP 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!