What is the difference between DOMXPath::evaluate and DOMXPath::query?

后端 未结 2 1354
余生分开走
余生分开走 2020-12-19 07:52

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

2条回答
  •  无人及你
    2020-12-19 08:28

    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

    Select all p elements inside a div: //div//p

    Select all href attributes in a elements the current document: //a/@href

    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().

    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.

提交回复
热议问题