XSLT String Manipulation - Remove spaces in Field Value for use in URL - SharePoint Development & Administration + InfoPath

Tuesday, September 22, 2009

XSLT String Manipulation - Remove spaces in Field Value for use in URL

XSLT function sets the value of a parameter to the value
of the required field, after processing to remove spaces.

The parameter is then used to set part of url in the href attribute of an <a> tag.



<xsl:template name="dvt_1.groupheader0">
<xsl:param name="fieldtitle" />
<xsl:param name="fieldname" />
<xsl:param name="fieldvalue" />
....... other template params

1. Add a parameter to store

<xsl:param name="stringNoSpaces" >
<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="@Field-XPath" />
<xsl:with-param name="from" select="string(' ')" />
</xsl:call-template>
</xsl:param>

....... main template code

<a href="/FirstURLComponent/{$stringNoSpaces}"><xsl:value-of select="$fieldvalue" /></a>

....... remaining template code

</xsl:template>


2. Add string replace template after the main template:

<xsl:template name="stringreplace">
<xsl:param name="stringvalue" />
<xsl:param name="from" />

<xsl:choose>
<xsl:when test="contains($stringvalue, $from)"><xsl:value-of select="substring-before($stringvalue, $from)" />
<xsl:if test="contains(substring($stringvalue, 1, string-length($stringvalue) - 1), $from)">
<xsl:call-template name="stringreplace">
<xsl:with-param name="stringvalue" select="substring-after($stringvalue, $from)" />
<xsl:with-param name="from" select="$from" />
</xsl:call-template>
</xsl:if>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$stringvalue" /></xsl:otherwise>
</xsl:choose>
</xsl:template>





The value of the @Field-XPath field will processed by the string replace template to remove all spaces from the string.
The processed string is stored in the stringNoSpaces parameter to be used or displayed by the XSLT template
for a Data View Web Part.

Share this article:
Stumble This Delicious
Delicious
submit to reddit
Facebook
MySpace
MySpace

No comments:

Post a Comment