如何使用 xsl 转换具有属性而不是标签的 xml 站点地图?

Tho*_*son 5 xml xslt

我有一个简单的站点地图,其代码如下:

<?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>
Run Code Online (Sandbox Code Playgroud)

我可以使用以下 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>
Run Code Online (Sandbox Code Playgroud)

问题是,当我用谷歌测试我的站点地图时,它会给我所有无法识别的标签(标题和级别)的警告。我可以重写 xml 以使用每个属性而不是无法识别的标签,它看起来像:

<?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>
Run Code Online (Sandbox Code Playgroud)

我已经用谷歌测试过了,没有收到任何警告或错误。我的问题是如何将 xsl 重写为它在 html 中以与以前相同的方式显示的位置?

Dim*_*hev 6

这是对提供的转换的小调整

<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>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的“符合谷歌”的站点地图 XML 文档时:

<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>
Run Code Online (Sandbox Code Playgroud)

产生了想要的、正确的结果:

<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>
Run Code Online (Sandbox Code Playgroud)