您可以通過將站點地圖的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();
什麼是阻止你從開始編碼的實際阻止程序?或者你已經有一些代碼? – hakre
我更關心如何通過元素和遍歷來實現循環。我可以將輸出格式化爲列表/等等......只是不太確定如何安全地正確獲取數據。 –