Monday, February 1, 2010

value-of-select inside an xslt tag's attribute

I've been work with xslt lately and ran into an issue the other day while trying to put the value of a node inside a tag's attribute while in xslt. If you know a little about xslt...this will error out because you can't have a < or > inside the attribute. So what do you do? Well in the past I have been escaping the quotes in xslt and writing a ton of code to something so little...until now. Check it out:

Here is my xml that I want to loop over.

<cfsavecontent variable="xml">


<boarders>
<boarder>
<id>1</id>
<name>Joe</name>
<board>Forum</board>
</boarder>
</boarders>


</cfsavecontent>

<!---Here I am putting the value of the id node in the href attribute of an a tag by escaping quotes. Pretty gross huh?--->
<cfsavecontent variable="xslt">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:variable name="XML" select="/"/>
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="$XML">
<xsl:text disable-output-escaping="yes"><</xsl:text>a
href='test.cfm?id=
<xsl:value-of select="boarders/boarder/id"/>
'<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="boarders/boarder/name"/>
<xsl:text disable-output-escaping="yes"></a</xsl:text>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>

<cfoutput>
#xmltransform(xml,xslt)#
</cfoutput>


Now check this out. The same xslt with {} to access the node. Alot cleaner.


<cfsavecontent variable="xslt">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:variable name="XML" select="/"/>
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="$XML">

<a href="test.cfm?id={boarders/boarder/id}"><xsl:value-of select="boarders/boarder/name"/></a>

</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>
<a href="http://www.w3.org/TR/xslt#dt-attribute-value-template">http://www.w3.org/TR/xslt#dt-attribute-value-template</a><br/>
<cfoutput>
#xmltransform(xml,xslt)#
</cfoutput>


It, the {}, also supports xslt variables as well.


<cfsavecontent variable="xslt">
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:variable name="XML" select="/"/>
<xsl:template match="/">
<html>
<head></head>
<body>
<xsl:for-each select="$XML">

<xsl:variable name="boarder_Var">
<xsl:value-of select="boarders/boarder/id"/>
</xsl:variable>
a href="test.cfm?id={$boarder_Var}"><xsl:value-of select="boarders/boarder/name"/></a>

</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</cfsavecontent>

<cfoutput>
#xmltransform(xml,xslt)#
</cfoutput>


Use Case:
I wanted to merge some data into a set of links that the user clicks on that returns the value to an FCKeditor.

No comments:

Post a Comment