Properly Utilize XSLT Parameters with ColdFusion
ColdFusion 3 Comments »Often a CMS will store its content in a database as structured XML. The easiest way to display this data is often through a short XSLT, which ColdFusion can render a transform using the XmlTransform() function, introduced to ColdFusion in ColdFusion MX.
XSLT Parameter Usage
ColdFusion MX 7 introduced the ability to include parameters to the transform. Parameters allow you to pass in values to be rendered by the XSLT that are not included in the original XML. Following is an example of how an external parameter might be used with the following XML and XSLT:
-
<Content>
-
<Title>My Title</Title>
-
<Hyperlink>/articles/my-article-name/</Hyperlink>
-
</Content>
-
<?xml version="1.0"?>
-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl
utput method="xml" /> -
-
<xsl:template match="//Content">
-
<xsl:param name="baseWebUrl" />
-
<a>
-
<xsl:attribute name="href">
-
<xsl:value-of select="$baseWebUrl" /><xsl:value-of select="Hyperlink" />
-
</xsl:attribute>
-
<xsl:value-of select="Title" />
-
</a>
-
</xsl:template>
-
</xsl:stylesheet>
Injecting a Parameter
According to the documentation at the Adobe Knowledge Base, parameters are "a structure containing XSL template parameter name-value pairs to use in transforming the document. The XSL transform defined in the xslString parameter uses these parameter values in processing the XML."
Usually when I create structures in ColdFusion, I use the familiar dot shorthand notation:
-
<cfset parameters = StructNew()>
-
<cfset parameters.favoriteKitten = url.kitten />
-
<cfset parameters.numberOfKittens = numKittens />
-
<cfset parameters.baseWebUrl = application.webBase />
-
<cfset MyXmlContent = XmlTransform(MyXmlDocument,MyXsltDocument,parameters)>
Using this method, my code would consistently fail to pass in any of the parameters. Careful reading of the ColdFusion 7 comments for the XmlTransform function reveals to us that we must use associative array notation for parameters to work properly. It's as easy as switching the assignation of keys in the structure from dot notation above, to the associative array notation below:
-
<cfset parameters = StructNew()>
-
<cfset parameters["favoriteKitten"] = url.kitten />
-
<cfset parameters["numberOfKittens"] = numKittens />
-
<cfset parameters["baseWebUrl"] = application.webBase />
-
<cfset MyXmlContent = XmlTransform(MyXmlDocument,MyXsltDocument,parameters)>
Structure Notation Reasoning
Why is this the case? Why should it make a difference? Quite likely, as the commentor noted in the documentation, it is due to the syntax of ColdFusion. XML and XSLT allows for the "-" character to be included in variable names. Instead of baseWebUrl, I could have named the parameter base-web-url. Using ColdFusion and dot object notation would have resulted in <cfset parameters.base-web-url = application.webBase />, which obviously would result in an attempted mathematically equation and a ColdFusion error.
So, just remember, don't use dot notation! Use associative array notation, or if you wish, the StructInsert() function to add your structure keys.
I hope this saves you at least half of the hair-tearing-out that I endured.
Popularity: 9% [?]

Recent Comments