小编Tho*_*son的帖子

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

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

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

xml xslt

5
推荐指数
1
解决办法
3447
查看次数

标签 统计

xml ×1

xslt ×1