– Common Function Library Project

xslt(xmlSource, xslSource[, stParameters])

Last updated January 16, 2006


Mark Mandel

Version: 2 | Requires: CF6 | Library: DataManipulationLib

This UDF using CFMX's underlying Java XML/XSL engine to provide support for XSL transformations. Natively CFMX does not provide support for parameters to be passed through to XSL stylesheets, nor does it allow for relative use of <xsl:import> tags due to the fact that xsl files must first be read into memory via <CFFILE>. This UDF works around this by leveraging the underlying Java which supports parameter pass through, and also allows you enter a file path for your xml/xsl document. Syntax: XSLT(xmlsource, xslsource [, stParameters]) Arguments: xmlSource - either a valid xml document as a string, or a absolute file path to a XML file. xslSource - either a valid XSL document as a string, or a absolute file path to a XSL file. stParameters (optional) - a structure of xsl:param elements to pass through where the key is the name of the param, and the value is the value of the param being passed through. Do note that StructInsert() will need to be used as param names are case sensitive, and otherwise the struct key value will be in uppercase.

Return Values:
Returns a string.


<cfxml variable="xml">
<!--- valid xml doc --->

<cfxml variable="xsl">
<!--- valid xsl doc --->

 stParams = StructNew();
 StructInsert(stParams, "root", "");

<cfoutput>#xslt(xml, xsml, stParams)#</cfoutput>

<cfoutput>#xslt("c:\xmlFile.xml", xsl, stParams)#</cfoutput>


<cfoutput>#xslt(xml, "c:\xslFile.xsl", stParams)#</cfoutput>


<cfoutput>#xslt(("c:\xmlFile.xml", "c:\xslFile.xsl", stParams)#</cfoutput>


Name Description Required
xmlSource The XML Source. Yes
xslSource The XSL Source. Yes
stParameters XSL Parameters. No

Full UDF Source:

 Provides CFMX native XSL transformations using Java, with support for parameter pass through and relative &amp;lt;xsl:import&amp;gt; tags.
 Version 1 was by Dan Switzer
 @param xmlSource 	 The XML Source. (Required)
 @param xslSource 	 The XSL Source. (Required)
 @param stParameters 	 XSL Parameters. (Optional)
 @return Returns a string. 
 @author Mark Mandel ( 
 @version 2, January 16, 2006 
<cffunction name="xslt" returntype="string" output="No">
	<cfargument name="xmlSource" type="string" required="yes">
	<cfargument name="xslSource" type="string" required="yes">
	<cfargument name="stParameters" type="struct" default="#StructNew()#" required="No">
		var source = "";		var transformer = "";	var aParamKeys = "";	var pKey = "";
		var xmlReader = "";		var xslReader = "";		var pLen = 0;
		var xmlWriter = "";		var xmlResult = "";		var pCounter = 0;
		var tFactory = createObject("java", "javax.xml.transform.TransformerFactory").newInstance();
		//if xml use the StringReader - otherwise, just assume it is a file source.
		if(Find("<", arguments.xslSource) neq 0)
			xslReader = createObject("java", "").init(arguments.xslSource);
			source = createObject("java", "").init(xslReader);
			source = createObject("java", "").init("file:///#arguments.xslSource#");
		transformer = tFactory.newTransformer(source);
		//if xml use the StringReader - otherwise, just assume it is a file source.
		if(Find("<", arguments.xmlSource) neq 0)
			xmlReader = createObject("java", "").init(arguments.xmlSource);
			source = createObject("java", "").init(xmlReader);
			source = createObject("java", "").init("file:///#arguments.xmlSource#");
		//use a StringWriter to allow us to grab the String out after.
		xmlWriter = createObject("java", "").init();
		xmlResult = createObject("java", "").init(xmlWriter);		
		if(StructCount(arguments.stParameters) gt 0)
			aParamKeys = structKeyArray(arguments.stParameters);
			pLen = ArrayLen(aParamKeys);
			for(pCounter = 1; pCounter LTE pLen; pCounter = pCounter + 1)
				//set params
				pKey = aParamKeys[pCounter];
				transformer.setParameter(pKey, arguments.stParameters[pKey]);			
		transformer.transform(source, xmlResult);
		return xmlWriter.toString();
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson