If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

XPath : if there is element with the specified link

Started by Lechlak, Feb 03, 2023, 07:41 AM

Previous topic - Next topic

LechlakTopic starter

Hello.

You can find the sitemap.xml file below in the specified format:

<?xml version="1.0" encoding="UTF-8"?>
<urlset
   xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
       http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
 <loc>https://site.tu/index.html</loc>
 <changefreq>monthly</changefreq>
 <priority>0.90</priority>
</url>
<url>
 <loc>https://site.tu/map.html</loc>
 <changefreq>monthly</changefreq>
 <priority>0.90</priority>
</url>
<url>
 <loc>https://site.tu/intro.html</loc>
 <changefreq>monthly</changefreq>
 <priority>0.90</priority>
</url>
...
</urlset>

I am attempting to confirm the presence of an element that contains a specified URL link. Here's the code I'm using:

$sitemap=@simplexml_load_file($_SERVER['DOCUMENT_ROOT']."/sitemap.xml");
if (!$sitemap)
   showMessage("Error loading sitemap.xml",MT_ERROR);
...

$url="https://site.com/".$page;
$items=$sitemap->xpath("//url/loc[text()='".$url."']"]");

if (!$items)
   $str="Not present in sitemap.xml";

However, the absence message appears on all pages, while requests are being processed on http://xpather.com/. Can you help me determine what I am doing wrong?
  •  


cambridgesf

In order to register the namespace, refer to https://www.php.net/manual/en/domxpath.registernamespace.php. Alternatively, you can try using the following code:

$items=$sitemap->xpath("//*:url/loc[text()='".$url."']");
 Registering a namespace in PHP allows you to specify an alias or prefix for the namespace URI, making it easier to work with multiple namespaces in your code.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...