简体   繁体   English

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... 试着决定哪个更适合我的用例......

After comparing the documentation for these methods, my vague understanding is evaluate returns a typed result but query doesn't. 在比较这些方法的文档之后,我模糊的理解是evaluate返回一个类型化的结果,但query没有。 Furthermore, the query example includes looping through many results but the evaluate example assumes a single typed result. 此外, query示例包括循环遍历许多结果,但evaluate示例假定单个类型的结果。

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 - eg will the multiple/single results mentioned above always be the case? 当你使用其中一个时,任何人都可以解释(尽可能接近外行人的条款) - 例如,上面提到的多个/单个结果总是如此吗?

DOMXPath::query() supports only expressions that return a node list. DOMXPath :: query()仅支持返回节点列表的表达式。 DOMXPath::evaluate() supports all valid expressions. DOMXPath :: evaluate()支持所有有效表达式。 The official method is named evaluate(), too: http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator 官方方法也命名为evaluate(): http//www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator

Select all p elements inside a div : //div//p 选择div//div//p所有p元素

Select all href attributes in a elements the current document: //a/@href 选择所有href的属性, a元素在当前文档: //a/@href

You can use the string() function to cast the first element of a node list to a string. 您可以使用string()函数将节点列表的第一个元素强制转换为字符串。 This will not work with DOMXpath::query(). 这不适用于DOMXpath :: query()。

Select the title text of a document: string(/html/head/title) 选择文档的标题文本: 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. 您将始终知道结果的类型。

query will return a DOMNodeList regardless of your actual XPath expression. 无论您的实际XPath表达式如何, query都将返回DOMNodeList This suggests that you don't know what the result may be. 这表明您不知道结果可能是什么。 So you can iterate over the list and check the node type of the nodes and do something based on the type. 因此,您可以迭代列表并检查节点的节点类型 ,并根据类型执行某些操作。

But query is not limited to this use case. query不仅限于此用例。 You can still use this when you know what type you will get. 当你知道你会得到什么类型时,你仍然可以使用它。 It may be more readable in the future what you wanted to achieve and therefore easier to maintain. 它将来可能更具可读性,因此更易于维护。

evaluate on the other hand gives you exactly the type that you select. 另一方面, evaluate为您提供您选择的类型。 As the examples point out: 如例子所示:

$xpath->evaluate("1 = 0"); // FALSE
$xpath->evaluate("string(1 = 0)"); // "false"

As it turns out selecting attributes //div/@id or text nodes //div/text() still yields DOMNodeList instead of strings. 事实证明选择属性//div/@id或文本节点//div/text()仍会产生DOMNodeList而不是字符串。 So the potential use cases are limited. 因此潜在的使用案例是有限的。 You would have to enclose them in string : string(//div/@id) or text nodes string(//div/text()) . 您必须将它们括在stringstring(//div/@id)或文本节点string(//div/text())

The main advantage of evaluate is that you can get strings out of your DOMDocument with fewer lines of code. evaluate的主要优点是,您可以使用较少的代码行从DOMDocument获取字符串。 Otherwise it will produce the same output as query . 否则它将产生与query相同的输出。

ThW's answer is right that some expressions will not work with query : 我的答案是正确的,有些表达式不适用于query

$xpath->query("string(//div/@id)") // DOMNodeList of length 0
$xpath->evaluate("string(//div/@id)") // string with the found id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM