php - Use DOM and XPath to remove a node from a sitemap file -
i trying develop function removes url nodes sitemap file. here have far.
$xpath = new domxpath($domfile); $elements = $xpath->query("/urlset/url/loc[contains(.,'$pageurl')]"); echo count($elements); foreach($elements $element){ //this want delete url echo $element; echo "here".$element->nodevalue; } which outputs "111111". don't know why can't echo string in foreach loop if $elements count '1'.
up until now, i've been doing
$urls = $dom->getelementsbytagname( "url" ); foreach( $urls $url ){ $locs = $url->getelementsbytagname( "loc" ); $loc = $locs->item(0)->nodevalue; echo $loc; if($loc == $fullpageurl){ $removeurl = $dom->removechild($url); } } which work fine if sitemap wasn't big. times out right now, i'm hoping using xpath queries faster.
after gordon's comment, tried:
$xpath = new domxpath($domfile); $query = sprintf('/urlset/url[./loc = "%d"]', $pageurl); foreach($xpath->query($query) $element) { //this want delete url echo $element; echo "here".$element->nodevalue; } and not returning anything.
i tried going step further , used codepad, using used in other post mentioned, , did this:
<?php error_reporting(-1); $xml = <<< xml <?xml version="1.0" encoding="utf-8" ?> <url> <loc>professional_services</loc> <loc>5professional_services</loc> <loc>6professional_services</loc> </url> xml; $id = '5professional_services'; $dom = new domdocument; $dom->loadxml($xml); $xpath = new domxpath($dom); $query = sprintf('/url/[loc = $id]'); foreach($xpath->query($query) $record) { $record->parentnode->removechild($record); } echo $dom->savexml(); and i'm getting "warning: domxpath::query(): invalid expression" @ foreach loop line. other comment on urlset, i'll sure include double slashes in code, tried , returned nothing.
xml sitemap should :
<?xml version="1.0" encoding="utf-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc></loc> ... </url> <url> <loc></loc> ... </url> ... </urlset> since got namespace, query little more complicated previous answer :
$xpath = new domxpath($domfile); // here register namespace shortcut $xpath->registernamespace('sm', "http://www.sitemaps.org/schemas/sitemap/0.9"); // request should work $elements = $xpath->query('/sm:urlset/sm:url[sm:loc = "'.$pageurl.'"]'); foreach($elements $element){ // hint manual comments $element->parentnode->removechild($element); } echo $domfile->savexml(); i'm writing out of memory before going bed. if doesn't work i'll go test tomorrow morning. (and yes, i'm aware bring downvotes)
if don't have namespace (you should that's not obligation sigh)
$elements = $xpath->query('/urlset/url[loc = "'.$pageurl.'"]'); you got concrete example it's working here : http://codepad.org/vugl1mac
Comments
Post a Comment