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

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


private function _generateSitemapXml ($pages)

$sitemap = new Zend_View_Helper_Navigation_Sitemap();


$container = new Zend_Navigation();


// 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);




Popular posts from this blog

Enable pgpass under Windows 7

Numeric Drop-down With PHP ranges

PHPUnit 4 on Windows 7