Hi Michal
I’ve made a few changes and improvements to v 0.2, posted below, specifically adding some error handling and ability to get XSL from a modx document.
Would you like to add this to the repository?
<?php
/*
Snippet Name: xslt
Short Desc: Transform a remote XML feed using a local XSL file
Created By: Michal Charemza
Modified By: Nick Crossland
Last modified: 25/06/2007
Version: 0.3
License: GPL http://www.gnu.org/copyleft/gpl.html
Example call: [!xslt? &xmlURL=`http://www.domain.com/feed.rss` &xslFile=`[(filemanager_path)]/assets/templates/mytemplate/testStyle.xsl` &xslParams='someExampleParameter:3,otherParemeter:yes'!]
Parameters:
&xmlURL: URL of remote XML file to be transformed. If this contains ? or & or = characters, it will need to be URL encoded
&xslFile: Location of XSL file on the webserver. Can also be a ModX doc ID.
&xslParams: Comma separated list of parameterName:parameterValue pairs
that are given to the XSL processor
&error_msg: Message to be returned if the XML feed is unavailable
&log_errors: boolean - log any errors retrieving or parsing the XML into ModX's event log
*/
/***************************************************************************
* Preferences
****************************************************************************/
$error_msg = isset($error_msg)?$error_msg:'The feed is temporarily unavailable.';
$log_errors = isset($log_errors)?$log_errors:true;
/***************************************************************************
* Check which options specified in snippet call
****************************************************************************/
$xslParamsSpecified = isset($xslParams);
/***************************************************************************
* Process XSL Parameters from Snippet Call if given
****************************************************************************/
// Fill paramArray with variable name => value pairs
$paramArray = array();
if ($xslParamsSpecified) {
$keyValuePairs = explode(',', $xslParams);
$numParams = count($keyValuePairs);
for ($i = 0; $i <$numParams; $i += 1) {
$pair = explode(':', $keyValuePairs[$i]);
$pairArray = array($pair[0] => $pair[1]);
$paramArray = array_merge($paramArray, $pairArray);
}
}
/***************************************************************************
* Retrieve remote XML File
****************************************************************************/
// If the URL has been encoded
$xmlURL = urldecode($xmlURL);
// Create curl session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xmlURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Return xml string
$xmlDocString = curl_exec ($ch);
// Close curl session
curl_close($ch);
// Fix for some cases when an rss feed doesn't have a namespace (causes XML parser to fail)
$xmlDocString = str_replace('<rss version="2.0">', '<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">', $xmlDocString);
// Create domDocument object from XML string
$error = '';
if (!$xmlDoc = @domxml_open_mem($xmlDocString,DOMXML_LOAD_PARSING,&$error)) {
// Log the error in ModX's event log, if we have chosen to
if ($log_errors) {
$error_text = print_r($error, true);
$error_message = "
Error text:
$error_text
XML doc contents:
$xmlDocString
";
$modx->logEvent(0, 3, $error_message, 'XSL snippet parsing error');
}
// Return a friendly error message to the user
return $error_msg;
}
/***************************************************************************
* Get local XSL File
****************************************************************************/
// Has a modx doc ID been supplied?
if (is_numeric($xslFile)) {
$doc_content = $modx->getDocument(intval($xslFile), 'content', 1);
$xslDoc = domxml_xslt_stylesheet ($doc_content['content']);
} else {
// Create domDocument object from XSL file
$xslDoc = domxml_xslt_stylesheet_file ($xslFile);
}
/***************************************************************************
* Translate XML using XSL and given parameters
****************************************************************************/
$translated = $xslDoc->process($xmlDoc, $paramArray);
/***************************************************************************
* Output
****************************************************************************/
// Output translated XML to string
$output = $translated->dump_mem();
// Remove XML prologue
$output = strstr($output,"\n");
$output = substr($output, 1);
return $output;
?>