PHP + XPath with DOM/SimpleXML
Posted: Fri Sep 10, 2010 11:03 am
I´m using a sitemap in some arbitrary standard:
Lots of <url>s.
Why the fsck doesn´t this work:
I can run the same query in Javascript against the file and it works. Actually, i can run a bunch of different queries and they all work, like
just nothing with PHP using XPath with DOM or SimpleXML.
WHY?
Code: Select all
<?xml version="1.0"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://www.mysite.com/interesting-keyphrase</loc>
<lastmod>2010-09-09</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>Why the fsck doesn´t this work:
Code: Select all
$XMLfile = new DOMDocument;
$XMLfile->Load('/path/to/mysitemap.xml');
$sxml = new SimpleXMLElement('/path/to/mysitemap.xml', NULL, TRUE);
$XPath = new DOMXPath($XMLfile);
$keyphrases = array('interesting-keyphrase');
for($j = 0; $j < count($keyphrases); $j++)
{
$query = '//loc[text()="http://www.mysite.com/' . $keyphrases[$j] . '"]';
$entry = $XPath->query($query);
$entry2 = $sxml->xpath($query);
var_dump($entry);
var_dump($entry2);
if($entry->length > 0)
{
echo 'yay';
break;
}
}
$XMLfile = NULL;
$XPath = NULL;
Code: Select all
$query = '/urlset/url/loc[text()="http://www.mysite.com/' . $keyphrases[$j] . '"]'WHY?