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:

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% [?]

ColdFusion Checks String Length

ColdFusion 3 Comments »

It's such a simple thing but I actually googled this because I couldn't think of it offhand.

CODE:
  1. <cfif len(str)>
  2.      <cfoutput>#str#</cfoutput>
  3. </cfif>

if str has zero characters, len(str) will return 0, mimicking behaviour of false. Cool!

Popularity: 27% [?]

Mini-CF Site

CSS, ColdFusion, Vida, Web Dev No Comments »

So I said I was done with school. That's not completely true. I am taking one more course this semester, but I'm not attending it; I am only doing the work.

The CDSource inspired ColdFusion site is for the ColdFusion course I am taking. Feel free to play around with it, but the cart isn't working yet so don't do that.

The admin section has no policing yet, so you can check that out too. Go ahead and add products.

I had fun writing the CSS for it. I am not a designer, but I like bringing design to reality. This was one of the cleanest stylesheet that I have written, although it was not difficult considering that there really isn't that much to style.

I'm slowing getting into this JavaScript filled Web2.0 world before me. I avoided JavaScript like the plague before. Now it's fun to make little things dynamic, such as the field on the product-type change to CD. So so very simple, but it makes me inexplicably giddy.

Popularity: 8% [?]

Handling 404 Errors for a Migrated Blog

ColdFusion, Vida 1 Comment »

Handling 404 Errors for a Migrated Blog by mi hermano, Joshua, displays my brother's particular genius and allows the entire world to see it not only in the ColdFusion Developer's Journal e-zine, but in the printed magazine as well.

Congratulations, Josh! Wahoo!

Popularity: 6% [?]

  • Special Thanks To:Romow Web Directory & WordPress Themes
    Entries RSS Log in