Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use xsl to transform a xml sitemap that has attributes instead of tags?

Tags:

xml

xslt

I have a simple sitemap that has code like:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc>home page link</loc>
    <title>East Randolph Cabinet Shop</title>
    <level>level-1</level>
</url>
.
.
</urlset>

I can easily transform to display the way I want on a webpage with the follow xsl:

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">

<xsl:template match="/">
<h2>Sitemap</h2>
<p>You can use our sitemap to easily navigate to any section of our website. If you still cannot find the information you need don't hesitate to <a href="contact.php">contact</a> us.</p>
<ul>
    <xsl:for-each select="sm:urlset/sm:url">
        <li class="{sm:level}">
            <a href="{sm:loc}"><xsl:value-of select="sm:title"/></a>
        </li>
    </xsl:for-each>
</ul>
</xsl:template>

</xsl:stylesheet>

The problem is that when I test my sitemap with google it gives me warnings for all the unrecognized tags (title and level). Can I rewrite the xml to use attributes for each instead of unrecognized tags to where it would look something like:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc title='East Randolph Cabinet Shop' level='level-1'>home page link</loc>
</url>
.
.
</urlset>

I've tested this with google and get no warnings or errors. My question is how would I rewrite the xsl to where it would display in html the same way as before?

like image 501
Thomas Ellison Avatar asked Apr 15 '13 01:04

Thomas Ellison


1 Answers

This is a small adjustment of the provided transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">

 <xsl:template match="/">
    <html>
        <h2>Sitemap</h2>
        <p>You can use our sitemap to easily navigate to any section of our website. If you still cannot find the information you need don't hesitate to <a href="contact.php">contact</a> us.</p>
        <ul>
            <xsl:for-each select="sm:urlset/sm:url/sm:loc">
                <li class="{@level}">
                    <a href="{.}"><xsl:value-of select="@title"/></a>
                </li>
            </xsl:for-each>
        </ul>
    </html>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided "google-compliant" sitemap XML document:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc title='East Randolph Cabinet Shop' level='level-1'>home page link</loc>
    </url>
.
.
</urlset>

the wanted, correct result is produced:

<html xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">
   <h2>Sitemap</h2>
   <p>You can use our sitemap to easily navigate to any section of our website. If you still cannot find the information you need
      don't hesitate to <a href="contact.php">contact</a> us.
   </p>
   <ul>
      <li class="level-1"><a href="home page link">East Randolph Cabinet Shop</a></li>
   </ul>
</html>
like image 64
Dimitre Novatchev Avatar answered Oct 16 '22 23:10

Dimitre Novatchev