From c8c4af8aa8e7334a16ad9898e430c16d26f6dcb6 Mon Sep 17 00:00:00 2001 From: Mathew Davies Date: Tue, 25 Jan 2011 16:03:53 +0000 Subject: [PATCH] Experiment with XSLT. --- classes/kohana/sitemap.php | 45 +++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/classes/kohana/sitemap.php b/classes/kohana/sitemap.php index 3813f99..02a53ef 100644 --- a/classes/kohana/sitemap.php +++ b/classes/kohana/sitemap.php @@ -11,6 +11,13 @@ class Kohana_Sitemap * @var DOMElement */ protected $_root; + + /** + * @var XSLTProcessor + */ + protected $_xslt; + + protected $_styles = array(); /** * Setup the XML document @@ -19,6 +26,9 @@ public function __construct() { // XML document $this->_xml = new DOMDocument('1.0', Kohana::$charset); + + // XSLT + $this->_xslt = new XSLTProcessor; // Attributes $this->_xml->formatOutput = TRUE; @@ -44,6 +54,20 @@ public function add( Sitemap_URL $object ) $this->_root->appendChild($this->_xml->importNode($url, TRUE)); } + /** + * Add an XSL to the root XML document. + * + * @param string $url + * @param constant Bitwise OR of the libxml option constants. + * @return Sitemap + */ + public function add_stylesheet($url, $options = NULL) + { + $this->_styles[] = array('location' => $url, 'options' => $options); + + return $this; + } + /** * Ping web services * @@ -122,6 +146,7 @@ public static function encode($string) } /** + * * Format a unix timestamp into W3C Datetime * * @access public @@ -154,9 +179,23 @@ public static function date_format($unix) */ public function render() { - // Default uncompressed - $response = $this->_xml->saveXML(); - + if (empty($this->_styles)) + { + $response = $this->_xml->saveXML(); + } + else + { + foreach($this->_styles as $style) + { + $xsl = new DOMDocument; + $xsl->load($style['location'], $style['options']); + + $this->_xslt->importStylesheet($xsl); + } + + $response = $this->_xslt->transformToXML($this->_xml); + } + if ($this->gzip) { // Try and gzip the file before we send it off.