PHP + XPath with DOM/SimpleXML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
triumphantwogs
Forum Newbie
Posts: 1
Joined: Fri Sep 10, 2010 10:53 am

PHP + XPath with DOM/SimpleXML

Post by triumphantwogs »

I´m using a sitemap in some arbitrary standard:

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>
Lots of <url>s.

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;
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

Code: Select all

$query = '/urlset/url/loc[text()="http://www.mysite.com/' . $keyphrases[$j] . '"]'
just nothing with PHP using XPath with DOM or SimpleXML.

WHY?
Post Reply