当我包含 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 时 XSLT 不起作用 [英] XSLT does not work when I include xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
问题描述
我的 Google 站点地图通过 XSLT 可以很好地呈现,而 < 中没有 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"urlset > 元素,但是当包含时,我的 foreach 语句不起作用,模板中没有任何内容.我的代码在下面.感谢您的帮助.
My Google sitemap renders well through XSLT fine without the xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" in the < urlset > element, however when included, my foreach statement doesn't work and nothing renders in the template. My code's below. Thanks for your help.
XML
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>{site_url}</loc>
<lastmod>{current_time format="%Y-%m-%d"}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
</urlset>
XSL
<xsl:template match="/">
<html>
<body>
<h2>Sitemap</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Location</th>
<th>Last Modified</th>
<th>Update Frequency</th>
<th>Priority</th>
</tr>
<xsl:for-each select="urlset/url">
<tr>
<td><xsl:value-of select="loc"/></td>
<td><xsl:value-of select="lastmod"/></td>
<td><xsl:value-of select="changefreq"/></td>
<td><xsl:value-of select="priority"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
推荐答案
我的 Google 站点地图呈现良好XSLT 很好,没有
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
但是,在元素中包括在内时,我的 foreach 语句不起作用,没有任何渲染模板
My Google sitemap renders well through XSLT fine without the
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
in the<urlset>
element, however when included, my foreach statement doesn't work and nothing renders in the template
这是一个常见问题.
XPath 将任何不带前缀的名称视为属于无命名空间".但是,提供的文档中的元素属于 "http://www.sitemaps.org/schemas/sitemap/0.9"
命名空间 -- 而不是 "no namespace"
.
XPath treats any unprefixed name as belonging to "no namespace". However, the elements in the provided document belong to the "http://www.sitemaps.org/schemas/sitemap/0.9"
namespace -- not to "no namespace"
.
因此,以下 XPath 表达式根本不选择任何节点:
Therefore, the following XPath expression doesn't select any node at all:
urlset/url
解决方案:
在 XSLT 样式表中定义 "http://www.sitemaps.org/schemas/sitemap/0.9"
命名空间并为其关联前缀.然后将此前缀与参与任何 XPath 表达式的所有名称一起使用.
Define the "http://www.sitemaps.org/schemas/sitemap/0.9"
namespace in the XSLT stylesheet and associate a prefix to it. Then use this prefix with all names that participate in any XPath expression.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9"
exclude-result-prefixes="s"
>
<xsl:template match="/">
<html>
<body>
<h2>Sitemap</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Location</th>
<th>Last Modified</th>
<th>Update Frequency</th>
<th>Priority</th>
</tr>
<xsl:for-each select="s:urlset/s:url">
<tr>
<td><xsl:value-of select="s:loc"/></td>
<td><xsl:value-of select="s:lastmod"/></td>
<td><xsl:value-of select="s:changefreq"/></td>
<td><xsl:value-of select="s:priority"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>{site_url}</loc>
<lastmod>{current_time format="%Y-%m-%d"}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
</urlset>
它正确地产生以下结果:
<html>
<body>
<h2>Sitemap</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Location</th>
<th>Last Modified</th>
<th>Update Frequency</th>
<th>Priority</th>
</tr>
<tr>
<td>{site_url}</td>
<td>{current_time format="%Y-%m-%d"}</td>
<td>monthly</td>
<td>0.5</td>
</tr>
</table>
</body>
</html>
这篇关于当我包含 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 时 XSLT 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!