사이트 맵의 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
저는 요소를 통과하고 탐색하는 방법에 더 관심이 있습니다. 출력을 목록으로 형식화 할 수 있습니다./etc ... 데이터를 안전하게 안전하게 얻는 방법을 잘 모르겠습니다. –