我的XML站点地图的XSL转换使用php不能工作


xsl transform of my xml sitemap is not working using php

我有一个简单的sitemap.xml文件:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <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>

然后我有我的站点地图。xsl文件:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
     <h2>Sitemap</h2>
     <ul>
     <xsl:for-each select="urlset/url">
      <li class="&lt;xsl:value-of select=&quot;level&quot;/&gt;"><a href="&lt;xsl:value-of select=&quot;loc&quot;/&gt;"><xsl:value-of select="title"/></a></li>
     </xsl:for-each>
    </ul>
    </xsl:template>
    </xsl:stylesheet>

sitemap。php文件中的代码将xml文件转换为xsl文件然后返回结果

<div id="content">
<?php
$xml = new DOMDocument;
$xml->load('sitemap.xml');
$xsl = new DOMDocument;
$xsl->load('sitemap.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
</div>

当我将浏览器指向Sitemap .php文件时,所有回显的内容都是直到Sitemap标题<h2>Sitemap</h2>。我对xsl非常陌生,所以请原谅我的无知,但在我看来,xsl for-each语句有问题。我说的对吗?我被这事困住了。

我发现你的XSL文件有两个问题。

  1. 你没有为站点地图文件使用正确的XML命名空间
  2. 你不了解属性值模板

应该像下面这样工作:

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

注意sitemap XML命名空间和属性值模板的sm前缀