Answer the question
In order to leave comments, you need to log in
PHP: How to get parent, multiple children from html with XPath and keep their order in DOM tree?
Hello!
Parsing html and trying to parse it with XPath
$html = file_get_contents($url);
//echo $html;
$document = new DOMDocument();
$document->loadHTML($html);
$xpath = new DOMXpath($document);
$elements = $xpath->query('//div[contains(@class,"versions")]');
echo '<pre>';
print_r($elements['elements']);
echo '<pre>';
<div class='versions'>
<div>..</div>
<a class='versions-item'></a>
</div>
Answer the question
In order to leave comments, you need to log in
One XPath request or how. We will have to write a couple of queries: 1) get //div[contains(@class,"versions")] a collection of div nodes that we make the context node for 2) query //a[contains(@class,"versions-item")].
The code would be something like this:
$elements = $xpath->query('//div[contains(@class,"versions")]');
foreach ($elements as $contextNode) {
$versionsItemNode = $xpath->query('//a[contains(@class,"versions-item")]', $contextNode);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question