当我包含xmlns ="http://www.sitemaps.org/schemas/sitemap/0.9"时,XSLT不起作用

Jul*_*ian 12 xml sitemap xslt xml-namespaces google-sitemap

我的Google站点地图通过XSLT很好地呈现,而没有<urlset>元素中的xmlns ="http://www.sitemaps.org/schemas/sitemap/0.9",但是当包含时,我的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>
Run Code Online (Sandbox Code Playgroud)

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

Dim*_*hev 17

我的Google站点地图xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 在没有<urlset>元素的情况下通过XSLT很好地渲染 ,但是当包含时,我的foreach语句不起作用,模板中没有任何渲染

这是一个FAQ.

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

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

urlset/url
Run Code Online (Sandbox Code Playgroud)

方案:

"http://www.sitemaps.org/schemas/sitemap/0.9"在XSLT样式表中定义名称空间并将前缀与其关联.然后将此前缀与参与任何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>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的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>
Run Code Online (Sandbox Code Playgroud)

它正确地产生以下结果:

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