PDA

View Full Version : XSLT not working!


mlse
05-14-2007, 05:02 PM
Hi all, I have an issue concerning the formatting of sitemaps using XSLT in order to make them more human-readable.

Firstly, here's my test sitemap (small for ease of reading!):


<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="sitemap.xsl" ?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
<url>
<loc>http://127.0.0.1/</loc>
<lastmod>2007-05-13</lastmod>
</url>
</urlset>


And here is my XSLT:


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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Sitemap for http://127.0.0.1/</h2>
<table>
<tr><td>Link</td><td>Lastmod</td></tr>
<xsl:for-each select="urlset/url">
<tr>
<td><xsl:value-of select="loc" /></td>
<td><xsl:value-of select="lastmod" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Check it out in a text editor - the for-each loop doesn't seem to result in any output to the browser. *HOWEVER* If the xmlns attribute of the urlset tag is removed in the target XML then the for-each loop works as you would expect!

Any ideas?

shyam
05-15-2007, 09:51 AM
<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:u="http://www.google.com/schemas/sitemap/0.84">
<xsl:template match="/">
<html>
<body>
<h2>Sitemap for http://127.0.0.1/</h2>
<table>
<tr><td>Link</td><td>Lastmod</td></tr>
<xsl:for-each select="u:urlset/u:url">
<tr>
<td><xsl:value-of select="u:loc" /></td>
<td><xsl:value-of select="u:lastmod" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

mlse
05-16-2007, 11:26 AM
Thanks for that! :thumbsup:

Out of interest, what specifically does the xmlns:u instruct the XSLT "engine" to do and why is it required?

mlse
05-16-2007, 11:33 AM
No worries! I've figured it out with a little thought!

I presume that the u is just an alias to the google sitemaps namespace in the sitemap document? (And so it could be x or a or anything you wanted?).

Arbitrator
05-16-2007, 12:23 PM
I presume that the u is just an alias to the google sitemaps namespace in the sitemap document? (And so it could be x or a or anything you wanted?).Correct. You can use xmlns:prefix to give any namespace a custom prefix. xmlns:xsl could be changed to xmlns:coolstuff or the more precise xmlns:xslt, if you wanted. On the other hand, a plain xmlns declaration will set a default namespace for the current and child elements.