Create Sitemap XML file


posted by uknzguy
01-21-2009

Downloads: 481
File size: 2.9kB
Views: 3,345

Embed
Create Sitemap XML file
  1. #################################################################################################### 
  2. ## Write-Sitemap.ps1 
  3. ## 
  4. ## Generates a basic Sitemap for your website, based on a list of locations / products or whatever, 
  5. ## (depending on your requirements). Can easily be extended to create more complex Sitemaps. 
  6. ##  
  7. ## This uses XMLTextWriter and is based on the RSS creator script at  
  8. ## http://pshscripts.blogspot.com/2008/12/write-rssstreamps1.html 
  9. ## 
  10. #################################################################################################### 
  11. ## Sitemap format, as output by this script (see http://www.sitemaps.org/protocol.php for more) 
  12. ## 
  13. ## <?xml version="1.0" encoding="utf-8"?> 
  14. ## <urlset xmlns="http://www.google.com/schemas/sitemap/0.9"> 
  15. ##  <url> 
  16. ##   <loc>http://your.url/here/product1</loc> 
  17. ##  </url> 
  18. ##  <url> 
  19. ##   <loc>http://your.url/here/product2</loc> 
  20. ##  </url> 
  21. ## </urlset> 
  22. #################################################################################################### 
  23.  
  24. # Let's Set up some variables 
  25. $workingdir = "c:\scripts" 
  26. $Domain     = "http://www.somesite.url"        # Base URL - CHANGE THIS! 
  27. $path         = "$workingdir\sitemap.xml"        # Where the output file will go 
  28.  
  29.  
  30. $Statics     = @("help", "contact", "terms", "privacy")     # Any static links go here 
  31. $Locations     = Get-Content "$workingdir\products.txt"    # A list of product IDs 
  32.  
  33. $Counter     = 0        # Keep track of how many URLs we include. Must be less than 50k per file. 
  34.  
  35. # This function does the bulk of the work, creating a new <url> and <loc> element for each URL 
  36. function CreateElement([string]$url
  37.     $w = $global:writer 
  38.     $w.WriteStartElement("url"
  39.     $w.WriteStartElement("loc"
  40.     $w.WriteString($url
  41.     $w.WriteEndElement() #end loc 
  42.     $w.WriteEndElement() #end url 
  43.  
  44.     $global:counter++    # Increment URL counter 
  45.  
  46. #### MAIN ##### 
  47.  
  48. # Set up encoding, and create new instance of XMLTextWriter 
  49. $encoding = [System.Text.Encoding]::UTF8 
  50. $writer = New-Object System.Xml.XmlTextWriter( $Path, $encoding
  51. $writer.Formatting = [system.xml.formatting]::indented 
  52.  
  53. Write-Host "`r`n`r`nGenerating $Domain Sitemap... $path" 
  54.  
  55. # Write start of XML document 
  56. $writer.WriteStartDocument() 
  57.  
  58. # Write Start of sitemap doc 
  59. $writer.WriteStartElement( "urlset"
  60. $writer.WriteAttributeString( "xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"
  61.  
  62. # Write our static URLs 
  63. foreach ($static in $statics
  64.     CreateElement "$Domain/$Static" 
  65.  
  66. # Write all location URLs 
  67. foreach ($Location in $Locations
  68.     $r = "$domain/product/$location" 
  69.     CreateElement $r 
  70.  
  71. # Write end of document information 
  72. $writer.WriteEndElement() # End urlset 
  73.  
  74. # Make sure we close the file 
  75. $writer.close() 
  76.  
  77. # Let's see what it has done 
  78. # cat $path 
  79.  
  80. # See how many elements were output 
  81. Write-Host "`n`rTotal number of URLs : $Counter" 
  82. # If this is more than 50,000 we should split into multiple sitemap files... 

If you have a medium to large size website, you can provide a Sitemap so that search engines intelligently index your site's content.

A Sitemap in it's simples form is an XML file containing a list of available URLs on your site. See http://sitemaps.org for more information on Sitemaps.

This script is intended as a starting point for generating a Sitemap for your site - since every website is different you will most likely need to customise it to make it work for you.

In it's present form, this script generates a list of static URLs based on the contents of an array and also a list of product / location / items from a text file. This can be expanded as necessary to suit your requirements.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.