(view source code of comparesitemaps_php as plain text)
<?php
// comparesitemaps.php, version 1.00
// Compare 2 XML based sitemap files
// Modify the values below and then run this PHP file on a web server (e.g. XAMPP)
// Written by Rob van der Woude
// https://www.robvanderwoude.com/
// Allow the script to run up to 15 minutes
set_time_limit( 900 );
// Modify file names to match your situation, add paths if not in the current directory
$sitemap1 = "sitemap1.xml";
$sitemap2 = "sitemap2.xml";
// Make verbose TRUE to show ALL entries, or FALSE to show DIFFERENCES ONLY
$verbose = true;
?><html>
<head>
<title>Compare XML sitemap files</title>
<style type="text/css" type="text/css">
td, th {
border: 1px solid silver;
padding: 0.5em 1em;
}
/* Make the table header "stick"" */
thead {
position: sticky;
inset-block-start: 0;
background: rgba(222, 222, 222, 1);
}
.Green {
color: green;
}
.Red {
color: red;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th><?php print( basename( $sitemap1 ) ); /* use realpath instead of basename if the file */ ?></th>
<th>URL</th>
<th><?php print( basename( $sitemap2 ) ); /* names are equal and the folders are different */ ?></th>
</tr>
</thead>
<tbody>
<?php
$xmlorg = simplexml_load_file( $sitemap1 ) or die( "Cannot load " . $sitemap1 );
$xmlbak = simplexml_load_file( $sitemap1 ) or die( "Cannot load " . $sitemap2 );
// register namespace to make XPath work at all
// https://stackoverflow.com/a/1246023
$xmlbak->registerXPathNamespace( 'sm', "http://www.sitemaps.org/schemas/sitemap/0.9" );
ob_flush( );
flush( );
foreach ( $xmlorg->children( ) as $url ) {
$loc = $url->loc[0];
$lastmodorg = $url->lastmod[0];
$lastmodbak = $xmlbak->xpath( '/sm:urlset/sm:url[sm:loc="' . $loc . '"]/sm:lastmod' );
if ( is_array( $lastmodbak ) ) {
$lastmodbak = implode( '', $lastmodbak );
}
if ( $verbose ) {
if ( $lastmodorg == $lastmodbak ) {
print( "<tr class=\"Green\">\n" );
} else {
print( "<tr class=\"Red\">\n" );
}
print( "\t<td>{$lastmodorg}</td>\n" );
print( "\t<td>{$loc}</td>\n" );
print( "\t<td>{$lastmodbak}</td>\n" );
print( "</tr>\n" );
ob_flush( );
flush( );
} else {
if ( $lastmodorg != $lastmodbak ) {
print( "<tr>\n" );
print( "\t<td>{$lastmodorg}</td>\n" );
print( "\t<td>{$loc}</td>\n" );
print( "\t<td>{$lastmodbak}</td>\n" );
print( "</tr>\n" );
ob_flush( );
flush( );
}
}
}
?>
</tbody>
</table>
</body>
</html>
page last modified: 2024-04-16; loaded in 0.0234 seconds