JavaScript snippet to analyze XML sitemap

Trying to make sense of large XML sitemaps isn’t easy. Thankfully, I’ve got your back with the power of JavaScript.

You can use the snippet below to instantly generate a pivot table showing a breakdown of a website’s path segments based on the URLs found in the XML sitemap. The table can help you identify the sections most popular in a site and its publication date range. It’s useful for your website analysis or carrying out competitor research.

How to use the XML sitemap script

Open any XML sitemap and run the code below in the DevTools Console Log (CTR+SHIFT+J).

The script will open a new tab with an HTML table that breaks down the XML sitemap into individual path segments.

The ‘Total URLs’ column shows the number of URLs found in each path segment. The ‘Open URLs’ button will reveal the individual URLs inside each group using an accordion functionality.

If the <lastmod> attribute is found in the XML sitemap, the ‘From’ and ‘To’ columns will show the earliest and latest ‘Last Modified’ dates from the group.

Any path segment with only 1 URL is excluded from the table.

JavaScript code snippet

Copy-paste the minified JS code below:

(function(){var e=window.location.href;fetch(e).then(t=>t.text()).then(t=>{var n=new DOMParser,r=n.parseFromString(t,"text/xml"),o=r.getElementsByTagName("url"),u={};for(var a=0;a<o.length;a++){var l=o[a].getElementsByTagName("loc")[0].textContent,c=o[a].getElementsByTagName("lastmod")[0],s=c?new Date(c.textContent):null,d=s?formatDate(s):null,p=new URL(l).pathname,f=p.split("/").filter(Boolean);f.forEach(function(t){u[t]? (u[t].count++,u[t].urls.push(l),s&&(s<u[t].from&&(u[t].from=s),s>u[t].to&&(u[t].to=s))):(u[t]={count:1,from:s,to:s,urls:[l]})})}var g=Object.keys(u).filter(function(e){return u[e].count>1}).map(function(e){return[e,u[e].count,u[e].from,u[e].to,u[e].urls]});g.sort(function(e,t){return t[1]-e[1]});var m="<style>table{border-collapse:collapse;width:100%;}th,td{padding:8px;text-align:left;}th{background-color:black;color:white;}tr:nth-child(even){background-color:#efefef;}.url-list{display:none;}</style>";m+="<h2>Breakdown of XML Sitemap by path segments</h2><table border='1'><tr><th>Path Segments</th><th>Total URLs</th>"+(g[0][2]?"<th>From</th><th>To</th>":"")+"<th>Individual URLs</th></tr>",g.forEach(function(e,t){var n=t%2==0?"white":"#efefef";m+="<tr style='background-color:"+n+";' class='segment-row'><td>"+e[0]+"</td><td>"+e[1]+"</td>"+(e[2]? "<td>"+formatDate(e[2])+"</td><td>"+formatDate(e[3])+"</td>":"")+"<td><a style='cursor:pointer;color:white;background-color:#444;border:none;padding:6px 12px;border-radius:2px;text-decoration:none;' href='#' class='open-link' data-segment='"+e[0]+"'>Open URLs</a><div class='url-list'>",e[4].forEach(function(t){m+="<tr class='url-row' style='display:none;' data-segment='"+e[0]+"'><td colspan='"+(e[2]?5:3)+"'><a href='"+t+"' target='_blank'>"+t+"</a></td></tr>"}),m+="</div></td></tr>"}),m+="</table>";var w=window.open();w.document.write(m),w.document.close();var b=w.document.querySelectorAll(".open-link");b.forEach(function(e){e.addEventListener("click",function(t){t.preventDefault();var n=e.getAttribute("data-segment"),r=w.document.querySelectorAll(".url-row[data-segment='"+n+"']");r.forEach(function(e){e.style.display=e.style.display==="none"?"table-row":"none"}),e.textContent==="Open URLs"?(e.textContent="Close URLs",e.style.backgroundColor="#FFA500",e.style.color="black"):(e.textContent="Open URLs",e.style.backgroundColor="#444",e.style.color="white")})});var y=w.document.createElement("div");y.style.fontSize="11px",y.style.marginTop="24px";var k=w.document.createElement("span");k.textContent="Created by Ziggy Shtrosberg @ ";var E=w.document.createElement("a");E.textContent="shtros.com",E.href="https://www.shtros.com/",E.style.color="inherit",E.style.textDecoration="underline";var v=w.document.createTextNode(" (because sharing is caring) ❤️");y.appendChild(k),y.appendChild(E),y.appendChild(v);var D=w.document.querySelector("body");D.appendChild(y)}).catch(function(e){console.error("Error fetching XML sitemap:",e),alert("Error fetching XML sitemap. Please make sure the URL is correct and accessible.")});function formatDate(e){var t={day:"2-digit",month:"short",year:"numeric"};return e.toLocaleDateString("en-US",t)}})();

Feel free to connect on LinkedIn and give me a shout if you find the snippet useful in your day-to-day SEO workflow.