{"id":1325,"date":"2024-04-08T13:23:03","date_gmt":"2024-04-08T13:23:03","guid":{"rendered":"https:\/\/www.shtros.com\/?p=1325"},"modified":"2024-04-08T13:23:05","modified_gmt":"2024-04-08T13:23:05","slug":"javascript-snippet-for-xml-sitemap","status":"publish","type":"post","link":"https:\/\/www.shtros.com\/javascript-snippet-for-xml-sitemap\/","title":{"rendered":"JavaScript snippet to analyze XML sitemap"},"content":{"rendered":"\n

Trying to make sense of large XML sitemaps isn\u2019t easy. Thankfully, I\u2019ve got your back with the power of JavaScript.<\/p>\n\n\n\n

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.<\/p>\n\n\n\n

How to use the XML sitemap script<\/h2>\n\n\n\n

Open any XML sitemap and run the code below in the DevTools Console Log (CTR+SHIFT+J).<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n
<\/div>\n\n\n\n

The script will open a new tab with an HTML table that breaks down the XML sitemap into individual path segments.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n
<\/div>\n\n\n\n

The \u2018Total URLs\u2019 column shows the number of URLs found in each path segment. The \u2018Open URLs\u2019 button will reveal the individual URLs inside each group using an accordion functionality.<\/p>\n\n\n\n

If the <lastmod> attribute is found in the XML sitemap, the ‘From’ and \u2018To\u2019 columns will show the earliest and latest ‘Last Modified’ dates from the group.<\/p>\n\n\n\n

Any path segment with only 1 URL is excluded from the table.<\/p>\n\n\n\n

JavaScript code snippet<\/h3>\n\n\n\n

Copy-paste the minified JS code below:<\/p>\n\n\n\n

(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) \u2764\ufe0f\");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)}})();<\/pre>\n\n\n\n

Feel free to connect on LinkedIn<\/a> and give me a shout if you find the snippet useful in your day-to-day SEO workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"

Trying to make sense of large XML sitemaps isn\u2019t easy. Thankfully, I\u2019ve 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 … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":1328,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","inline_featured_image":false,"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/posts\/1325"}],"collection":[{"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/comments?post=1325"}],"version-history":[{"count":3,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/posts\/1325\/revisions"}],"predecessor-version":[{"id":1334,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/posts\/1325\/revisions\/1334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/media\/1328"}],"wp:attachment":[{"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/media?parent=1325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/categories?post=1325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shtros.com\/wp-json\/wp\/v2\/tags?post=1325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}