Title: Find next Div Class name after parent div class name, using php/dom/xpath?
Tags: php;dom;xpath
Question: I have a known Div Class name, and I can retrieve the inner html code all good, but how would I retrieve the next Div Class name (not inner from the known Div Class) using php, dom document and xpath?
For example with the code below, if I know the Div class &quot;mobile-container mobile-filter-container&quot;, how would I return &quot;mobile-container mobile-cart-content-container&quot;?
<div class=&quot;mobile-container mobile-filter-container&quot;&gt;
<div class=&quot;mobile-wrapper-header&quot;&gt;</div&gt;
<div class=&quot;mobile-filter-wrapper&quot;&gt;</div&gt;
</div&gt;
<div class=&quot;mobile-container mobile-cart-content-container&quot;&gt;
<div class=&quot;mobile-wrapper-header&quot;&gt;
Thanks,
Comment: With the first div class, for my case I know it's name already and I get it's contents by the included function which works ( I am passing the dom document as a function parameter), but I don't know the name of the next and I'm having a difficult time trying to understand how to get it thanks: function Bn($dom){
$xpath = new DOMXPath($dom);
$classname = "knownClassname";
$div = $xpath->query("//div[@id='" . $classname . "']");
$div = $div->item(0);
$bn = $dom->saveHtml($div);
return $bn;

Comment: Can you add how you actually get the first div and what you have tried to get the second one?

Here is the accepted answer: I believe this should get you close enough to what you need:
$data = <<<DATA
<html&gt;
<div class=&quot;mobile-container mobile-filter-container&quot;&gt;
<div class=&quot;mobile-wrapper-header&quot;&gt;</div&gt;
<div class=&quot;mobile-filter-wrapper&quot;&gt;</div&gt;
</div&gt;
<div class=&quot;mobile-container mobile-cart-content-container&quot;&gt;
<div class=&quot;mobile-wrapper-header&quot;&gt;</div&gt;
<div class=&quot;mobile-filter-wrapper&quot;&gt;</div&gt;
</div&gt;
<div class=&quot;unwanted&quot;&gt;
<div class=&quot;mobile-wrapper-header&quot;&gt;</div&gt;
<div class=&quot;mobile-filter-wrapper&quot;&gt;</div&gt;
</div&gt;
</html&gt;
$doc = new DOMDocument();
$doc-&gt;loadHTML($data);
$xpath = new DOMXpath($doc);
$elements = $xpath-&gt;query('.//div[@class=&quot;mobile-container mobile-filter-container&quot;]/following-sibling75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0iv[1]/@class');
echo $elements[0]-&gt;nodeValue;
Output:
mobile-container mobile-cart-content-container
Comment for this answer: Thanks Jack, exactly what I was looking for :)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:11/05/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><dom><xpath>', 'Title': 'Find next Div Class name after parent div class name, using php/dom/xpath?', 'ViewCount': 269, 'id': 64695323, 'user_AboutMe': None, 'user_AccountId': 19902398.0, 'user_DisplayName': 'TonyC', 'user_Reputation': 47.0, 'user_Views': 5.0, 'user_id': 14582131.0}}