M
M
Mykola2015-02-12 18:39:32
PHP
Mykola, 2015-02-12 18:39:32

How to search in a previously found xpath element?

There is a page with a tabular layout where the characters are located.
Code for selecting the entire table.

//h2[text()="Characters & Voice Actors"]/following-sibling::table[position() < last()]//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]

Next, you need to select the current characters
//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]

And they already have the role
div/small
and name of the character from the link
/a
How to use the results of the prefetch?
To avoid doing a search through the entire document every time:
//h2[text()="Characters & Voice Actors"]/following-sibling::table[position() < last()]//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]/div/small/text()

//h2[text()="Characters & Voice Actors"]/following-sibling::table[position() < last()]//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]/a

$extracted = $anime->getFilterXPath('//h2[text()="Characters & Voice Actors"]/following-sibling::table[position() < last()]');
echo iterator_count($extracted); // 39

$extracted2 = $anime->getFilterXPath('//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]', $extracted);
echo iterator_count($extracted2); //68

$extracted3 = $anime->getFilterXPath('//h2[text()="Characters & Voice Actors"]/following-sibling::table[position() < last()]//tr/td[contains(@class, "borderClass bgColor") and 2 = position()]');
echo iterator_count($extracted3); //39

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2015-02-19
@iSensetivity

DOMNode is passed to the query method as the second parameter, inside which something needs to be found. The documentation has a good example php.net/manual/en/domxpath.query.php

K
krypt3r, 2015-02-13
@krypt3r

one.

How to use prefetch results?

You misspelled the word "samples". It was necessary to "select".
2. Use XPath for HTML parsing? Well, well, what if it's invalid?
2. Read the documentation on the getFilterXPath() method to see if it has a $contextnode parameter. If not, stomp towards DOMXPath.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question