Question

I am trying my hand at XSLT using this page as a guide: http://www.w3schools.com/xml/xml_xsl.asp

I basically tried to modify the given XSL information to suit my tags, which happen to be in the form of a sitemap. However, when i apply the XSL style to my XML page, it shows as blank! Clearly i am missing/forgetting or not seeing something. Please help.

My XML (snippet only):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl" ?>
<!-- sitemap-generator-url="http://www.auditmypc.com/free-sitemap-generator.asp" -->
<!-- This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp -->
<!-- Audit My PC also offers free security tools to help keep you safe during internet travels -->
<urlset 
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
>
<url>
<loc>http://www.site.com/</loc>
<lastmod>2014-03-17T13:30:25-05:00</lastmod>
</url>
<url>
<loc>http://www.site.com/index.php?format=feed&#x26;type=rss</loc>
<lastmod>2014-03-17T13:28:20-05:00</lastmod>
</url>
</urlset>

My XSL:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

<xsl:for-each select="urlset/url">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="loc"/></span>
</div>

<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<span>Last Modified:<xsl:value-of select="lastmod"/></span>
</div>
</xsl:for-each>

</body>
</html>

Thanks for the help!

Was it helpful?

Solution

The urlset element and its descendants in your example XML document are all in the http://www.sitemaps.org/schemas/sitemap/0.9 namespace, so in order to match them with XPath expressions you must bind a prefix to that namespace and use it consistently:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">

    <xsl:for-each select="sm:urlset/sm:url">
      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of select="sm:loc"/></span>
      </div>

      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <span>Last Modified:<xsl:value-of select="sm:lastmod"/></span>
      </div>
    </xsl:for-each>

  </body>
</html>

In XSLT/XPath 1.0, unprefixed element names always mean elements in no namespace, and prefixed names take the namespace bindings from the stylesheet, not from the input XML.

As an aside, if you're writing XSLT to be interpreted on the client side by browsers then you will be restricted to XSLT 1.0, so I suggest you find a good 1.0-specific tutorial and reference guide. The material on w3schools isn't always clear about what version of XSLT/XPath it refers to and you'll get frustrated if you keep trying things that don't work, only to find they're 2.0-only constructs...

OTHER TIPS

Remember, XPath (and therefore XSLT) is namespace-aware, but (in the 1.0 version of these standards at least) does not have the concept of default namespace (xmlns=) assignment. If you want to select namespaced nodes, your XPath must use explicit prefixes bound to the correct namespaces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top