I am sure this is well documented elsewhere, but it is something that always flummoxes me when I am working on a Data View in SharePoint Designer 2007. Typically you will want to add a link to the field in one of the columns in the data view so users can view the detail. One way to achieve this is with Web Part Connections that allow you to pass filter values to web parts on this or other pages. However I was looking for a simple hyperlink that would open the list item view using the ID of the record.
<td><xsl:value-of select="@ows_LinkTitle"/></td>
However simply trying to use the concat function with strings of html fails to parse
<xsl:value-of select="concat(‘<a href="/research/resportal/events/Lists/Events/DispForm.aspx?ID=’, @ows_ID, ‘">’, @ows_LinkTitle, ‘</a>’)" />
There are two solutions to this
- using the attribute tag: probably the right way to do this
- creating a variable: almost certainly a lot slower, but more flexible
The Attribute Tag
The facility to add attributes to an <a> is built into XSLT
1234567 <a><xsl:attribute name="href">http://www.bbc.co.uk</xsl:attribute><xsl:attribute name="onClick">javascript:alert('<xsl:value-of select?@FileDirRef? />')</xsl:attribute><xsl:value-of select?@FileLeaf? /></a>
This approach elegantly mixes HTML and values
Creating a variable
The trick is to declare the HTML strings as variables and then concat these variables
<td class="ms-vb"> <xsl:variable name="L1Text"><a href="/Events/DispForm.aspx?ID=</xsl:variable> <xsl:variable name="L2Text">"></xsl:variable> <xsl:variable name="L3Text"></a></xsl:variable> <xsl:value-of select="concat($L1Text, @ows_ID, $L2Text, @ows_LinkTitle, $L3Text)" disable-output-escaping="yes" /> </td>
The only two tricks are
- all HTML tags in the strings need to be escaped, i.e. &
- set disable-output-escaping="yes"
As the variables are static they could be declared earlier in the XSL so the are not reset on each iteration.