【问题标题】:XSLT does not work when I include xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"当我包含 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 时,XSLT 不起作用
【发布时间】:2011-04-19 15:57:05
【问题描述】:

如果 元素中没有 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9",我的 Google 站点地图可以通过 XSLT 很好地呈现,但是在包含时,我的 foreach 语句不起作用并且模板中没有任何内容。我的代码在下面。感谢您的帮助。

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>

【问题讨论】:

  • 好问题 (+1)。请参阅我的答案以获得解释和完整的解决方案。

标签: xml xslt xml-namespaces xml-sitemap


【解决方案1】:

xpath 需要命名空间作为前缀,例如

{http://www.sitemaps.org/schemas/sitemap/0.9}urlset

如果是 xmlns:x="http://www.sitemaps.org/schemas/sitemap/0.9" 你可以使用

x:urlset

看来这个页面对http://msdn.microsoft.com/en-us/library/ms950779.aspx有帮助

编辑:我打算发布它并跟进一个如何使用 xsl 定义前缀的示例,但 Dimitre 已经有了。

【讨论】:

    【解决方案2】:

    我的 Google 站点地图可以很好地通过 XSLT 没有 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 但是在&lt;urlset&gt; 元素中 包括在内时,我的 foreach 声明 不起作用,没有任何渲染 模板

    这是一个常见问题解答

    XPath 将任何无前缀的名称视为属于“无命名空间”。但是,提供的文档中的元素属于 "http://www.sitemaps.org/schemas/sitemap/0.9" 命名空间——而不是 "no namespace"

    因此,以下 XPath 表达式根本不选择任何节点:

    urlset/url
    

    解决方案

    在 XSLT 样式表中定义 "http://www.sitemaps.org/schemas/sitemap/0.9" 命名空间并为其关联一个前缀。然后将此前缀与参与任何 XPath 表达式的所有名称一起使用。

    <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>
    

    【讨论】:

    • 优秀的答案!谢谢!你的解释很清楚,解决方案奏效了!
    • 你知道如何将 {site_url} 变成可点击的 url。当我尝试下面的代码时,我收到以下错误 - “XML Parsing Error: not well-formed Location: sitename.com/sitemapxsl Line Number 194, Column 26:”,箭头指向 xsl 语法的第一个左大括号 -代码如下——" "
    • @Julian:这是另一个常见问题解答 :)。使用:&lt;a href='{s:loc}'&gt;
    • 让你摇滚!再次感谢!我还有另一个问题:)。对我来说,我对 XSLT 真的很陌生,两天前就遇到了它。无论如何,我注意到 IE 特定规则不起作用,如下所示。如何让 IE 6 及更高版本来处理它们。代码如下 - '
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签