So I have a HTML string like this:
<td class="name">
<a href="/blah/somename23123">Some Name</a>
</td>
<td class="name">
<a href="/blah/somename28787">Some Name2</a>
</td>
Using XPath I'm able to get value of href attribute using this Xpath query:
$domXpath = new \DOMXPath($this->domPage);
$hrefs = $domXpath->query("//td[@class='name']/a/@href");
foreach($hrefs as $href) {...}
And It's even easier to get a text value, like this:
// Xpath auto. strips any html tags so we are
// left with clean text value of a element
$domXpath = new \DOMXPath($this->domPage);
$names = $domXpath->query("//td[@class='name']/");
foreach($names as $name) {...}
Now I'm curious to know, how can I combine those two queries to get both values with only one query (If it's something like that even posible?).
To reduce the code to a single loop, try:
As per above :) Too slow ..
Simplest way,
evaluate
is for this task!The simplest way to obtain a value is by
evaluate()
method:Note: important to limit XPath returns to 1 item (the first
a
in this case), and cast the value withstring()
orround()
, etc.So, in a set of multiple items, using your
foreach
code,PS: this example is only for
evaluate
's illustration... When the information already exists at the node, use what offers best performance, as methodsgetAttribute()
,saveXML()
, etc. and properties as$nodeValue
,$textContent
, etc. supplied byDOMNode
.See @Gordon's answer for this particular problem.
The XPath subquery (at context) is good for complex cases — or symplify your code, avoiding to check hasChildNodes() + loop for $childNodes, etc. with no significative gain in performance.
Fetch
and then pluck the text with
nodeValue
and the attribute withgetAttribute('href')
.Apart from that, you can combine Xpath queries with the Union Operator
|
so you can useas well.