Parsing a Google Sitemap with jQuery

A Google Sitemap has the following form:

<?xml version="1.0" encoding="UTF-8"?>


<!-- more url elements -->


Here we're interested in the loc and lastmod elements which return the URI of the documents and the date of their last modification, respectively. First, let's set up some basic styles:

body {
    margin: 0 auto;
    width: 60%;
    padding: 2em 0;
    background: #fff;
    color: #333;
    font: 76% Arial, sans-serif;

a:link, a:visited {color: #080;}

h1 {
    font: normal 1.6em "Trebuchet MS", Trebuchet, sans-serif;
    color: #666;
    margin: 5px 0;
    padding-bottom: 3px;
    border-bottom: 3px solid #999;
    text-transform: uppercase;

#sitemap {
    margin: 1em 0;
    padding: 5px;
    border: 2px solid #c4df9b;
    background: #edf5e1;
    list-style: none;
    font-size: 1.1em;

#sitemap li {
    margin-bottom: 6px;

#sitemap li div.lastmod {
    font-family: Verdana, sans-serif;
    font-size: small;
    height: 100%;
    margin-bottom: 4px;
    padding: 3px 0;
    border-top: 1px dashed #666;
    border-bottom: 1px dashed #666;
    font-style: italic;

Then we add jQuery. To accomplish our task, we first need an helper function to parse the dates provided in the sitemap. It is as follows:

function formatDate(timestamp) {

   var dateParts = timestamp.split('-');
   var year = dateParts[0];
   var rawMonth = dateParts[1];
   var month;
   var day = dateParts[2];
   switch(rawMonth) {
       case '01':
         month = 'January';
       case '02':
         month = 'February';
       case '03':
         month = 'March';
       case '04':
         month = 'April';
       case '05':
         month = 'May';
       case '06':
         month = 'June';
        case '07':
         month = 'July';
       case '08':
         month = 'August';
       case '09':
         month = 'September';
        case '10':
         month = 'October';
       case '11':
         month = 'November';
       case '12':
         month = 'December';

   return month + ' ' + day + ',' + ' ' + year;  

This function parses a date in the format yyyy-mm-dd by splitting it into three different parts which will be later returned in the format 'm d, y'. Now it's time to use jQuery to parse our sitemap effectively:

$(document).ready(function() {

    $('<ul id="sitemap"></ul>').insertAfter('h1');

    $.get('sitemap.xml', function(xml) {
        $(xml).find('loc').each(function() {
 var rawUrl = $(this).text();
 var rawLastMod = $(this).parent().find('lastmod').text();
 var $url;
 var $lastMod;
 var timestamp;
 var $li = $('<li></li>');
 if(rawUrl.indexOf('index.html') != -1) {
      $url = rawUrl.replace('index.html', '');
 } else {
      $url = rawUrl;
 timestamp = rawLastMod.replace(/T.+/g, '');
 $lastMod = formatDate(timestamp);
 $li.html('<a href="' + $url + '">' + $url + '</a>' + '<div class="lastmod">Last Modified: ' + $lastMod + '</div>');


We use the $.get() method to fetch and parse our sitemap file. During parsing, we make sure that URLs passed in don't contain the string 'index.html'. If so, we remove it. We also make sure that the last modification date passed to the formatDate() function is in the format 'yyyy-mm-dd'. To do this, we remove the last part of the lastmod text node beginning with a 'T'. You can see the final result here.

Leave a Reply

Note: Only a member of this blog may post a comment.