开发者

Looking for a great SimpleXML tutorial (XML to UL>LI List) [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and mo开发者_运维问答re. You can edit the question so it can be answered with facts and citations.

Closed 8 years ago.

Improve this question

I've been looking around for a few days for a nice clean PHP SimpleXML tutorial.

I want to mimic the XML Sitemap of .NET and use a single sitemap xml file to drive my primary navigation, page titles and so on using PHP.

Here's an example of the XML structure: http://msdn.microsoft.com/en-us/library/yy2ykkab.aspx

(but maybe it should mimic a standard sitemap.xml for search engine purposes?)

As an intro, I just want to simply build a multi-level UL > LI for navigation.

Most tutorials I've looked at don't seem to apply and it seems like a very useful idea.

Thanks for any pointers!


You can solve your problem by transforming the XML of the sitemap into the XML of your UL / LI based navigation. This can be done with XSLT. Here is an example:

XSLT is basically some definition how to perform the transformation that is run in a processor. In the following code, $xslStr contains the stylesheet (defines the transformation) and $xmlStr contains the xml of the sitemap:

$xslt = new XSLTProcessor(); 
$xslt->importStylesheet(new  SimpleXMLElement($xslStr)); 
echo $xslt->transformToXml(new SimpleXMLElement($xmlStr));

The output looks like this:

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

The magic is basically inside the XSL, so here it is:

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

The full example:

$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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜