Using EXSLT To Extend XSLT With Custom Functions In ColdFusion
Posted November 11, 2009 at 10:26 AM by Ben Nadel
The other day, I answered a question for a reader about filtering XML nodes against a value list using the XPath function, contains(). In the question, the reader mentioned the EXSLT library; I had not heard of this, so when researching the solution, I looked it up at www.exslt.org. As it turns out, there's a whole mechanism and existing library for extending the functionality of XSLT and XPath. And, what's more awesome is that the XSLT engine that ColdFusion uses (Xalan) supports this extensioning.
One of the XSLT extensions offered in the EXSLT library is the "func:function" node. This node allow programmers to define their own XSLT-based custom functions that can be called within their XML transformations. As a launch pad for experimentation with this stuff, I figured I would readdress my previous blog post, but this time, try to solve the problem using EXSLT and a custom function.
If you don't remember from my previous post, the reader needed to filter an XML node by checking its attribute value against a list of target values. To solve this the first time, I hacked the contains() function to work as a list function (mostly); but, using EXSLT, I wanted to try and create an actual list function, list-find(), that would return true if a given value could be found in a given list. Here's what I came up with:
- <!--- Create our XML data source. --->
- <cfxml variable="girls">
- <girl type="cute">
- <girl type="athletic">
- <girl type="hot">
- <girl type="cute">
- <!--- Create the XSL tranformation. --->
- <cfsavecontent variable="xslt">
- <!--- Document type declaration. --->
- <?xml version="1.0" encoding="ISO-8859-1"?>
- When defining the Transform, you must add the namespace
- for the EXSL (XSL Extensions) functionality so that they
- can be properly resolved.
- NOTE: I am also adding my own name space, "ben", for use
- in the function name and information attributes.
- extension-element-prefixes="func str ben">
- The list-find() function allows us to search the
- given list for the given value. By default, the
- delimiters is the comma, but you can override with
- an optional argument.
- ben:hint="I take a delimited list and a value and return true or false as to whether that value is a list item in the given list.">
- <!--- Define arguments. --->
- ben:hint="I am the delimited list of values that is being searched."
- ben:hint="I am the value for which we are searching in the above list."
- ben:hint="I am the collection of delimiters used to deliniate the list values. I can be one or more characters (each character is used as a valid delimiter). I am defaulted to the comma."
- Using the EXSLT extension string library, split
- the list based on the given delimiters. This will
- return a node set of "token" nodes.
- Ex: str:tokenize( 'a,b,c', ',' )
- select="str:tokenize( $list, $delimiters )"
- Return a boolean-ized version of a query for token
- nodes who's text() value is equal to the list item
- value we are searching.
- Boolean() will return TRUE if the resultant node
- set returned from the select is non-empty.
- select="boolean( $tokens/text() = $value )"
- <!--- Match the root girls node. --->
- <xsl:template match="/girls">
- Copy each GIRL node if the TYPE attribute
- is contained within the given list: hot or
- NOTE: We are using a custom function as defined
- by the EXSLT func library.
- select="./girl[ ben:list-find( 'athletic,hot', @type ) ]"
- <!--- Transform the document. --->
- <cfset hotGirls = xmlParse(
- xmlTransform( girls, trim( xslt ) )
- ) />
- <!--- Output the new result. --->
- label="Hot Girls"
As you can see, my function "ben:list-find" takes 2 to 3 parameters - the list, the value in question, and an optional set of delimiters. Since this function is not part of the inherent XPath functionality, it needs to be name-spaced. Using the "ben" name space, I not only defined the function tag, I also defined many informational attributes that described the role of the function and its parameters (ala the attributes available in ColdFusion's CFFunction and CFargument tags). You know my style - the more insight, the better.
Running the above code, we get the following XML document:
| || || || || |
| || |
| || || |
As you can see, only the girl nodes who's @type attribute was in the list "athletic,hot" were returned in the resulting XML document. This is clearly a much better solution than my contains() approach.
What Other People Are Searching For
- Wanted: Full-Time ColdFusion Developer at Intoria Internet Architects
- Cold Fusion Senior Developer at Edge Information Management
- Back-End Web Developer-Information Technologist at Michigan State University
- ColdFusion Developer at Nonfat Media
- Mid-to-Senior Level Web Application Developer at SiteVision, Inc.
Nice post Ben, that is a handy feature
Yeah, it seems like a cool thing.
Is there some secret to using these exslt libraries?
I am trying to use one of their math functions math:power.
When ever I try to use it without a xsl:include line I get an error.
javax.xml.transform.TransformerException: ElemTemplateElement error: math:power
A Transformer object cannot be created that satisfies the configuration requested.
Here is my xsl:stylesheet tag:
My current ColdFusion is 9.0.
So I downloaded your hot girls example and it runs fine.
So it seems to be something about the math functions, particularly the math:power function that either ColdFusion or Xalan or both does not like.