Dec 19

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:

XML:
  1. <Content>
  2.    <Title>My Title</Title>
  3.    <Hyperlink>/articles/my-article-name/</Hyperlink>
  4. </Content>

XML:
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl :o utput method="xml" />
  4.  
  5. <xsl:template match="//Content">
  6. <xsl:param name="baseWebUrl" />
  7.    <a>
  8.       <xsl:attribute name="href">
  9.          <xsl:value-of select="$baseWebUrl" /><xsl:value-of select="Hyperlink" />
  10.       </xsl:attribute>
  11.    <xsl:value-of select="Title" />
  12.    </a>
  13. </xsl:template>
  14. </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:

CODE:
  1. <cfset parameters = StructNew()>
  2. <cfset parameters.favoriteKitten = url.kitten />
  3. <cfset parameters.numberOfKittens = numKittens />
  4. <cfset parameters.baseWebUrl = application.webBase />
  5. <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:

CODE:
  1. <cfset parameters = StructNew()>
  2. <cfset parameters["favoriteKitten"] = url.kitten />
  3. <cfset parameters["numberOfKittens"] = numKittens />
  4. <cfset parameters["baseWebUrl"] = application.webBase />
  5. <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% [?]

Share and Enjoy:
  • del.icio.us
  • Digg
  • Reddit
  • TwitThis
  • Google Bookmarks
  • StumbleUpon
  • Facebook
  • E-mail this story to a friend!
  • Print this article!

2 Responses to “Properly Utilize XSLT Parameters with ColdFusion”

  1. Josh Says:

    Doh! A good point to bear in mind! Thanks for the post.

  2. Seajay Says:

    I think that the problems is to do with case sensitivity... If you use dot notation, then the key name is stored in upper case within the structure, whereas using the associative array notation or the StructInsert() form will retain the case that you use.

    As the XML/XSLT is case sensitive it will fail if you end up with upper case parameter names when it's expecting otherwise.

Leave a Reply




  • Meta