View Single Post
  #10 (permalink)  
Old 05-13-2008, 10:38 AM
wige's Avatar
wige wige is offline
Moderator
WebProWorld Moderator
 
Join Date: Jun 2006
Location: United States
Posts: 2,629
wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9wige RepRank 9
Default Re: Attaching a style sheet to an xml sitemap

I have done something similar one one of my test domains, taking an entire web site of XML documents and using XSLT to convert the pages into web pages. If you want to see a practical working example, you can check out the site at TicketWarehouse.us. Although it is not an actual sitemap, if follows the same basic principles.

The first step is to add the link to the XSLT stylesheet in the sitemap itself:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://url.of/stylesheet.xsl" type="text/xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset>
The next step is to create the stylesheet itself. First, lay out the page you want the user to display in your favorite HTML editor. The code must be in HTML Strict, so that it can be put into the XSLT file, which is an XML document. The code must be fully standards compliant, as any errors could break the template. You should also keep the template as simple as possible. A very very basic XSLT file would be as follows:
Code:
<?xml version="1.0" encoding="ISO-8859-1"  ?> 
<xsl:stylesheet version="1.1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" /> 
<xsl:template match="/">
<html>
<head>
<title>Sitemap</title>
</head>
<body>
<h1>My Site Map</h1>
</body>
</html>
You will note the above code does not actually do anything with the data in the site map yet. That is the next step. First, you need to determine what elements you want converted into links. In an XML sitemap, links are stored in the <urlset> section, with each link represented by a <url> tag. However, since there is no title or description associated with these entries, all you could give the user is a long list of URLs, which link to your various pages. Use the following code (inserted after the </h1> tag in the code above) to list all of the URLs as links:

Code:
<xsl:template match="/urlset">
<xsl:for-each select="url">
<p>
<a href="{loc}">
<xsl:value-of select="loc" />
</a>
</p>
</xsl:for-each>
</xsl:template>
What this code snippet does is, if the XML file contains an element called "urlset", for each element within called "url", display the "loc" element as a link.
__________________
The best way to learn anything, is to question everything.
Reply With Quote