Styling a Sitemap Using XSL and Coldfusion - page 2

Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Step 2: Create the XSL File

We now create the xsl file we specified in our sitemap. To begin with we will create a simple un-styled map which will convert the listed nodes into a list of hyperlinks.

<xsl:stylesheet version="2.0" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<table cellpadding="5">
<tr style="border-bottom:1px black solid;">
<th>URL</th>
      <th>Priority</th>
      <th>Change Frequency</th>
      <th>LastChange</th>
</tr>
<xsl:for-each select="sitemap:urlset/sitemap:url">
      <tr>
<xsl:if test="position() mod 2 != 1">
       <xsl:attribute name="class">even</xsl:attribute>
       </xsl:if>
<td>
<xsl:variable name="itemURL">
<xsl:value-of select="sitemap:loc"/>
</xsl:variable>
<a href="{$itemURL}"><xsl:value-of select="sitemap:loc"/></a>
</td>
<td>
            <xsl:value-of select="concat(sitemap:priority*100,'%')"/>
       </td>
       <td>
            <xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),
             concat($lower, $upper),concat($upper, $lower)),
             substring(sitemap:changefreq, 2))"
/>

</td>
       <td>
            <xsl:value-of select="concat(substring(sitemap:lastmod,0,11),
concat(' ',substring(sitemap:lastmod,12,5)))"
/>

</td>
      </tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

There is alot going on here so lets break it down. If you want to skip the breakdown you can continue onto page 5.

Note: At this stage you should test if the xsl is working by visiting your sitemap. If it is working you should see a table of hyperlinks.

1 2 3 4 5 6 7

Reader Comments

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain advertisments or appear to be created for the purpose of link building, will not be published.

Archives Blog Listing