I have following HTML segment,
I am using xPath in PHP with DOM to fetch all the anchor tags inside the DIV using following xPath query:
//div[@id="breadcrumbs"]/a
I expect that the above query should return NodeList of all the anchor tags, which should be 3 in the case of above HTML. I am not getting anything and my following PHP code is skipping
$breadCrumb = $xpath->query('//div[@id="breadcrumbs"]/a');
if($breadCrumb->length){
$ctr = 0;
$sections = "";
foreach($breadCrumb as $section){
//$productBreadCrumb['section_'.$ctr] = $section->nodeValue;
$sections .= $section->nodeValue."|";
$ctr++;
}
$productData['sections'] = $sections;
}
I need guidance here. Very much appreciated.
EDIT: Added the page which I am trying to fetch from DOM based on xPath query on the above mentioned segment of Div id="breadcumbs"> a a a
http://www.5starhookah.com/Apex-Black-NEW-A342.htm
thanks
thanks
Your XPath is correct, as you can see if you use sample XHTML like:
<div id="breadcrumbs">
<a class='breadcrumb'>Our Products</a>
</div>
with a tool like: http://www.xpathtester.com/
Therefore, your issue must be with the DOM. If you could paste a printout of the DOM's XHTML from just before you run your XPath we could confirm as much, but it seems pretty clear that, barring something really strange, your problem is with the DOM.
Usual gotcha with xpath : the namespace.
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
means every tag is in fact in http://www.w3.org/1999/xhtml namespace.
Lookup the documentation to see if you can set a default namespace, or if you can alias it ( e.g. if the alias name is "x", the query will look like //x:div[@id="breadcrumbs"]/x:a .
Alternatively you could cheat by removing the string « xmlns="http://www.w3.org/1999/xhtml" » before parsing the xml.