Reputation: 857
How can one call a ColdFusion function, passing in attribute values as arguments, inside an XML transform template statement. For example, something like:
<xsl:template match="date">
<cfoutput>#DateFormat(now(), <xsl:value-of select="@format"/>)#</cfoutput>
</xsl:template>
Such that the following XML:
<date format="mm/dd/yy" />
Would be matched and transformed to the result of DateFormat(now(), "mm/dd/yy")
? Is it possible? I am able to do it with static arguments to DateFormat()
, cannot figure out how to extract a value from an attribute/node and use it as an argument. Thank you!
Update
Full version of current attempt:
<cfxml variable="xmlData">
<?xml version="1.0"?>
<date format="mm/dd/yy" />
</cfxml>
<cfxml variable="stylesheet">
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="date">
<cfoutput>#DateFormat(now(), '<xsl:value-of select="@format"/>')#</cfoutput>
</xsl:template>
</xsl:stylesheet>
</cfxml>
<cfoutput>#XmlTransform(xmlData, trim(stylesheet))#</cfoutput>
which results in the following error:
An error occured while Parsing an XML document. Element type "x2l:value-of" must be followed by either attribute specifications, ">" or "/>".
Upvotes: 2
Views: 1733
Reputation: 59311
Okay, here's what I think you're trying to do. You can't parse with XSLT and ColdFusion in one pass. You have to make two passes.
<cfxml variable="xmlData">
<?xml version="1.0"?>
<date format="mm/dd/yy" />
</cfxml>
<cfxml variable="stylesheet">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="date">
#DateFormat(now(), "<xsl:value-of select="@format"/>")#
</xsl:template>
</xsl:stylesheet>
</cfxml>
<cfset filename = "#createUUID()#.cfm" />
<cffile action="write" file="#getDirectoryFromPath(getCurrentTemplatePath())##filename#" output="#XmlTransform(xmlData, trim(stylesheet))#"/>
<cfinclude template="#filename#"/>
Upvotes: 3
Reputation: 59311
Looks like you just need quotes around the value.
<xsl:template match="date">
<cfoutput>#DateFormat(now(), '<xsl:value-of select="@format"/>')#</cfoutput>
</xsl:template>
Here's a complete stylesheet, which I tested with an online parser.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="date">
<cfoutput>#DateFormat(now(), '<xsl:value-of select="@format"/>')#</cfoutput>
</xsl:template>
</xsl:stylesheet>
And here's the XML code I used to test:
<?xml version="1.0"?>
<date format="mm/dd/yy" />
Upvotes: 0
Reputation: 19343
Why don't you use exslt datetime?
http://exslt.org/date/functions/format-date/index.html
Xalan supports it, possibly others, too.
Upvotes: 0
Reputation: 112210
You can use CFML to generate an XSL template.
You can also use an XSL template to turn appropriate XML into CFML (as in Patrick's answer).
However, these are two distinct operations, and cannot happen together at the same time (if you need both actions, you must do one then the other).
Upvotes: 2