Skip to main content

Insert processing istruction to Zend View Helper Navigation Sitemap domobject

What if you want to associate a xslt stylesheet to the sitemap generated by Zend_View_Helper_Navigation_Sitemap. I did it by adding a xml-stylesheet processing instruction to the view helper dom object. Here is the code:

class SitemapController extends Zend_Controller_Action
{
public function indexAction ()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender("true");

$pages = array();

// generate the pages array somehow
$someservice = new My_Serivce_PageGenerator();
$pages = $someservice->fetchAllMyWebsitePages();

// generate sitemap xml
$xml = $this->_generateSitemapXml($pages);

$response = $this->getResponse();
$response->setHeader('Cache-Control', 'public', true);
$response->setHeader('Content-Type', 'text/xml', true);
$response->appendBody($xml);

}

private function _generateSitemapXml ($pages)
{

$sitemap = new Zend_View_Helper_Navigation_Sitemap();

$sitemap->setView($this->view);
$sitemap->setUseSitemapValidators(false);
$sitemap->setMinDepth(0);
$sitemap->setMaxDepth(0);


$container = new Zend_Navigation();
$container->addPages($pages);

$sitemap->setContainer($container);


// get the dom object
$dom = $sitemap->getDomSitemap();

// define path to xslt stylesheet
$xslt_file_path = '/sitemap_style.xsl';

// create processing instruction
$xslt = $dom->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="' . $xslt_file_path . '"');

// add it to the dom
$urlset = $dom->getElementsByTagName('urlset');
$dom->insertBefore($xslt, $urlset->item(0));

// instead of using the render method we use this:
$xml = $sitemap->getUseXmlDeclaration() ? $dom->saveXML() : $dom->saveXML($dom->documentElement);

return rtrim($xml, PHP_EOL);


}

}

Comments

Popular posts from this blog

Enable pgpass under Windows 7

Last year I have written a post about enabling pgpass under Windows XP . Today I repeated the procedure for Windows 7. Basically everything worked as before, with the following exception - the APPDATA directory in Win 7 is different. What I did first was to check the name of the user running PostgreSQL server process. Then log in as that user and find out where is the APPDATA directory for that user. echo %APPDATA% From that point on, just follow the instructions in the previous post. Good luck!

PHPUnit 4 on Windows 7

Download phpunit.phar from - http://phpunit.de/ Find your php bin directory. In my case it is: C:\Program Files (x86)\Zend\ZendServer\bin Add this directory to your path. In the same php bin directory create .bat file with the following content: @ECHO OFF set PHP_BIN=php.exe set FILE_PATH="C:\path\to\phpunit_phar_file\phpunit.phar" %PHP_BIN% %FILE_PATH% %* Done. phpunit will be available from any location.