toomanyairmiles
toomanyairmiles

Reputation: 6485

Truncating text string in sharepoint

I'm using this script to truncate a text string in sharepoint 2007, but it doesn't work and I can't see why?! Any ideas offered greatly appreciated.

From Header.xsl

<xsl:template name="fixedstring">
<xsl:param name="targetVar">
<xsl:param name="allowablelength">
<xsl:value-of select="substring($targetVar, 1, $allowablelength)">
<xsl:if test="stringlength($targetVar) &gt; $allowablelength">
<xsl:text>...</xsl:text>
</xsl:if>
</xsl:value-of></xsl:param></xsl:param>
</xsl:template>

From ItemStyle.xsl

<xsl:call-template name="fixedstring">
<xsl:with-param name="targetVar">
<xsl:value-of select="@Reason_x005F_x0020_Not_x005F_x0020_Green"/>
<xsl:with-param name="allowablelength" select="50"></xsl:with-param>
</xsl:with-param>
</xsl:call-template>

Upvotes: 1

Views: 2740

Answers (3)

Wayferer
Wayferer

Reputation: 109

I know this is an old thread but it got me started on solving an issue and I thought I would put my results here for someone in the future.

We are using SharePoint 2010 Enterprise Search and for the results page I had the requirement of shortening the URL from the center and including the highlighting as well. The highlighting does not work when the URL is shortened however and there's probably an easier/better way to do this but this is what I did:

<span class="srch-URL2" id="{concat($currentId,'_Url')}" title="{$url}">
    <xsl:call-template name="truncateURL">
        <xsl:with-param name="targetURL">
            <xsl:value-of select="url"/>
        </xsl:with-param>
        <xsl:with-param name="allowablelength" select="number(40)"/>
    </xsl:call-template>
</span>

<xsl:template name="truncateURL">
    <xsl:param name="targetURL"/>
    <xsl:param name="allowablelength"/>
    <xsl:choose>
        <xsl:when test="string-length($targetURL) &lt; $allowablelength">
            <xsl:choose>
                <xsl:when test="hithighlightedproperties/HHUrl[. != '']">
                    <xsl:call-template name="HitHighlighting">
                        <xsl:with-param name="hh" select="hithighlightedproperties/HHUrl" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$targetURL"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="string-length($targetURL) &lt; ($allowablelength+$allowablelength)">
            <xsl:choose>
                <xsl:when test="hithighlightedproperties/HHUrl[. != '']">
                    <xsl:call-template name="HitHighlighting">
                        <xsl:with-param name="hh" select="hithighlightedproperties/HHUrl" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$targetURL"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="substring($targetURL, 1, $allowablelength)"/>
            <xsl:text>…</xsl:text>
            <xsl:value-of select="substring($targetURL, (string-length($targetURL)-$allowablelength)+1, $allowablelength)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Upvotes: 1

Welbog
Welbog

Reputation: 60438

OK, for starters, you're using nesting all wrong. Your param and with-param elements shouldn't be nested that way. Replace what you have with this:

<xsl:template name="fixedstring">
  <xsl:param name="targetVar"/>
  <xsl:param name="allowablelength"/>
  <xsl:value-of select="substring($targetVar, 1, $allowablelength)"/>
  <xsl:if test="string-length($targetVar) &gt; $allowablelength">
    <xsl:text>...</xsl:text>
  </xsl:if>
</xsl:template>

and

<xsl:call-template name="fixedstring">
  <xsl:with-param name="targetVar">
    <xsl:value-of select="@Reason_x005F_x0020_Not_x005F_x0020_Green"/>
  </xsl:with-param>
  <xsl:with-param name="allowablelength" select="50"/>
</xsl:call-template>

Note the string-length has a hyphen in it.

Upvotes: 3

Thiyagaraj
Thiyagaraj

Reputation: 3685

From my initial look, it looks like the "50" is not sent as a string, wrap it in single quotes.

<xsl:with-param name="allowablelength" select="'50'"></xsl:with-param>

or since it is a number, explicitly cast it as such

<xsl:with-param name="allowablelength" select="number(50)"></xsl:with-param>

Upvotes: 1

Related Questions