0

I have a folder where admin can upload images. Each group of images is related to a specific URL and tracked into a sitemap structure:

Basic XML sitemap

 <url>
   <loc>URL1</loc>
   <image:image>
     <image:loc>Image1 URL</image:loc>
     <image:caption>Image1 Caption</image:caption>
   </image:image>
   <image:image>
     <image:loc>Image2 URL</image:loc>
     <image:caption>Image2 Caption</image:caption>
   </image:image>
 </url> 

 <url>
    <loc>URL2</loc>
   <image:image>
     <image:loc>Image3 URL</image:loc>
     <image:caption>Image3 Caption</image:caption>
   </image:image>
 </url>

My goal is to update a specific URL data when admin uploads or delete images related to it. For example, if admin uploads a new Image4 and deletes Image1 from URL1, the updated sitemap will be:

Updated XML sitemap

 <url>
   <loc>URL1</loc>
   <image:image>
     <image:loc>Image2 URL</image:loc>
     <image:caption>Image2 Caption</image:caption>
   </image:image>
   <image:image>
     <image:loc>Image4 URL</image:loc>
     <image:caption>Image4 Caption</image:caption>
   </image:image>
 </url> 

 <url>
    <loc>URL2</loc>
   <image:image>
     <image:loc>Image3 URL</image:loc>
     <image:caption>Image3 Caption</image:caption>
   </image:image>
 </url>

I'm looking for an efficient way to perform updates. I've found this post, but (if I get it correctly) to update URL data, I need to iterate over all URL nodes, find the specific node (i.e. with regular expression) and then update data inside it (i.e. deleting all images and insert new images).

PHP

$dom = new DOMDocument('1.0');
$dom->load('images.xml');

$urls = $dom->getElementsByTagName('url');

foreach($urls as $url)
{
    $locs = $url->getElementsByTagName('loc');
    $loc = $locs->item(0)->nodeValue;

    // check $loc with regular expression and make updates to specific node
}

Honestly, I don't think it's an efficient solution. Is there a fast way to find a specific URL node and update images data inside it?

Community
  • 1
  • 1
Giorgio
  • 1,940
  • 5
  • 39
  • 64
  • what do you mean not efficient? are there thousands that needed an update? maybe you could but, you could use xpath to search for those only nodes that you want then make changes instead of looping all of them. – Kevin Jan 09 '15 at 08:13
  • @Ghost: no, only one URL node each time. My "not efficient" definition is related to the fact that if I have an URL node containing 100 images and admin deletes 1 image and uploads 1 new image, I must delete all images children and then insert updated images list inside node. The alternative is to parse each image child and check if it is still present in folder (and I don't think it's not efficient too). What I'm looking for is something equivalent to UPDATE MySQL command, but applied to XML. – Giorgio Jan 09 '15 at 08:21
  • you delete every child nodes inside image? why? you can just pick that one child and delete it, the mass delete node, i don't know if there is one. – Kevin Jan 09 '15 at 08:43
  • As you correctly point out, I would like to delete only deleted images and add only new images. But to perform this, I must parse each image child, right? How can I do this with DOMDocument? – Giorgio Jan 09 '15 at 08:52
  • 1
    No, you really don't need to somewhat select each image node. Just pick the ones that you needed deleted. You could utilize xpath for this case. Query the ones you needed to be deleted, then make the necessary deletion. Not looping each child and wait for that one inside the iteration then delete. – Kevin Jan 09 '15 at 09:04
  • 1
    @Giorgio instead of iterating, look into `xpath`to select the node with the one URL you need to update. – michi Jan 10 '15 at 01:45
  • Yes, I'm looking for xpath solution. Thanks for hints! – Giorgio Jan 10 '15 at 07:40

0 Answers0