DOMXPath :: evaluate和DOMXPath :: query有什么区别? [英] What is the difference between DOMXPath::evaluate and DOMXPath::query?
问题描述
尝试确定哪个更适合我的用例...
Trying to decide which is more appropriate for my use case...
比较这些方法的文档后,我的模糊的理解是 评估
返回类型的结果,但< a href =http://www.php.net/manual/en/domxpath.query.php =nofollow noreferrer> 查询
不T。此外, 查询
示例包括循环遍历许多结果,但 评估
的例子假设一个单一的打字结果。
After comparing the documentation for these methods, my vague understanding is evaluate
returns a typed result but query
doesn't. Furthermore, the query
example includes looping through many results but the evaluate
example assumes a single typed result.
仍然没有太多的聪明!当你使用一个或另一个时,任何人都可以解释(尽可能接近外行人的术语)。那么上面提到的多个/单个结果会一直是这样吗?
Still not much the wiser! Could anyone explain (in as close as possible to layman's terms) when you would use one or the other - e.g. will the multiple/single results mentioned above always be the case?
推荐答案
DOMXPath :: query()只支持返回节点列表。 DOMXPath :: evaluate()支持所有有效的表达式。官方方法也被命名为evaluate(): http: //www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator
DOMXPath::query() supports only expressions that return a node list. DOMXPath::evaluate() supports all valid expressions. The official method is named evaluate(), too: http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator
选择所有 p
元素 div
: // div // p
选择中的所有
href
属性元素当前文档: // a / @ href
您可以使用 string()
函数将节点列表的第一个元素转换为字符串。这将不适用于DOMXpath :: query()。
You can use the string()
function to cast the first element of a node list to a string. This will not work with DOMXpath::query().
选择文档的标题文本: string(/ html / head / title)
Select the title text of a document: string(/html/head/title)
还有其他功能和操作符可以更改表达式的结果类型。但它总是明确的。您将永远知道结果是什么类型。
There are other function and operators that will change the result type of an expression. But it is always unambiguous. You will always know what type the result is.
这篇关于DOMXPath :: evaluate和DOMXPath :: query有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!