One of the great frustrations working with XSLT in Data View Web Parts is the very limited set of functions available in XSLT 1.0 available in SharePoint 2007. However (as in so many things) javascript offers a solution.
This method is the simplest I have found and is based on posts at Sharepointalist, Programmingsharepoint and Sharepointboris.
Inserting the javascript functions
The method here works for inline scripts, but would probably also work with script references
- Locate the root XSLT template
- Insert the script within a CDATA tag
1 2 3 4 5 6 7 8 9 10 |
<xsl:template match="/"> <xsl:text disable-output-escaping="yes"> <![CDATA[ <script type="text/javascript"> function msg () { alert("hi"); } </script> ]]> </xsl:text> |
Calling the javascript functions
xsl:attribute
One method is to use xsl:attribute to build a link and add an onclick attribute
1 |
<a><xsl:attribute name="href">#</xsl:attribute><xsl:attribute name="onClick">javascript:msg()</xsl:attribute>test</a> |
using parameters and writing results back to template
This example takes the formatted modified date, passes it through a javascript function, and writes out the result
1 2 3 4 5 |
<td class="ms-vb"> <script language="javascript"> countup("<xsl:value-of select="ddwrt:FormatDate(string(@Modified),1033,1)" />"); </script> </td> |
The countup() javascript function ends with document.write(var); to output the result into the xslt
uriencode the page location for use the source element of a link
This would be particularly useful for making DVWPs portable and for working with InfoPath Form Libraries. See URI encode Source attribute in SharePoint 2007 Data View Web Part calling an InforPath form for a solution that uses these techniques.
1 |