寻找一个伟大的SimpleXML教程(XML到UL>LI列表)



几天来我一直在寻找一个干净的PHP SimpleXML教程。

我想模仿。net的XML Sitemap,使用一个单一的Sitemap XML文件来驱动我的主要导航,使用PHP的页面标题等。

下面是一个XML结构的例子:http://msdn.microsoft.com/en-us/library/yy2ykkab.aspx

(但也许它应该模仿一个标准的sitemap.xml搜索引擎的目的?)

作为介绍,我只想简单地构建一个用于导航的多级UL> LI。

我看过的大多数教程似乎都不适用,这似乎是一个非常有用的想法。

谢谢你的指点!

您可以通过将站点地图的XML转换为基于UL/LI的导航的XML来解决这个问题。这可以用XSLT完成。下面是一个例子:

XSLT基本上定义了如何执行在处理器中运行的转换。在下面的代码中,$xslStr包含样式表(定义转换),$xmlStr包含站点地图的xml:
$xslt = new XSLTProcessor(); 
$xslt->importStylesheet(new  SimpleXMLElement($xslStr)); 
echo $xslt->transformToXml(new SimpleXMLElement($xmlStr));

输出如下所示:

<ul>
  <li><a href="http://example.com/default.aspx" title="Home">Home</a><ul>
    <li><a href="http://example.com/Products.aspx" title="Our products">Products</a><ul>
      <li><a href="http://example.com/Hardware.aspx" title="Hardware choices">Hardware</a></li>
      <li><a href="http://example.com/Software.aspx" title="Software choices">Software</a></li>
    </ul></li>
    <li><a href="http://example.com/Services.aspx" title="Services we offer">Services</a><ul>
        <li><a href="http://example.com/Training.aspx" title="Training classes">Training</a></li>
        <li><a href="http://example.com/Consulting.aspx" title="Consulting services">Consulting</a></li>
        <li><a href="http://example.com/Support.aspx" title="Supports plans">Support</a></li>
    </ul></li>
  </ul></li>
</ul>

魔术基本上在XSL中,所以它是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  >
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template name="mapNode" match="siteMap">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>
  <xsl:template match="siteMapNode">
    <li>
      <a href="http://example.com{substring(@url, 2)}" title="{@description}">
        <xsl:value-of select="@title"/>
      </a>
      <xsl:if test="siteMapNode">
        <xsl:call-template name="mapNode"/>
      </xsl:if>
    </li>
  </xsl:template>
</xsl:stylesheet>

完整示例:

$xslStr = <<<XSL
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  >
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template name="mapNode" match="siteMap">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>
  <xsl:template match="siteMapNode">
    <li>
      <a href="http://example.com{substring(@url, 2)}" title="{@description}">
        <xsl:value-of select="@title"/>
      </a>
      <xsl:if test="siteMapNode">
        <xsl:call-template name="mapNode"/>
      </xsl:if>
    </li>
  </xsl:template>
</xsl:stylesheet>
XSL;
$xmlStr = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<siteMap>
  <siteMapNode title="Home" description="Home" url="~/default.aspx">
    <siteMapNode title="Products" description="Our products"
      url="~/Products.aspx">
      <siteMapNode title="Hardware" description="Hardware choices"
        url="~/Hardware.aspx" />
      <siteMapNode title="Software" description="Software choices"
        url="~/Software.aspx" />
    </siteMapNode>
    <siteMapNode title="Services" description="Services we offer"
        url="~/Services.aspx">
        <siteMapNode title="Training" description="Training classes"
          url="~/Training.aspx" />
        <siteMapNode title="Consulting" description="Consulting services" 
          url="~/Consulting.aspx" />
        <siteMapNode title="Support" description="Supports plans" 
          url="~/Support.aspx" />
    </siteMapNode>
  </siteMapNode>
</siteMap>
XML;
$xslt = new XSLTProcessor(); 
$xslt->importStylesheet(new  SimpleXMLElement($xslStr)); 
echo $xslt->transformToXml(new SimpleXMLElement($xmlStr)); 
return;
$name = 'home';
$page = $xml->xpath(sprintf("/content/page[@name='%s'][1]", $name));
if (!$page)
{
    throw new Exception(sprintf('Page "%s" not found.', $name));
}
list($page) = $page;
echo $page->asXML();

最新更新