1

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>
3
  • 1
    "It doesn't work" isn't a very specific thing. What doesn't work? What is it doing, and what do you expect it to do? Commented Sep 17, 2009 at 14:53
  • Basically the webparts ItemStyle feeds refuse to render.. Commented Sep 17, 2009 at 14:57
  • I expect the it to truncate the string "@Reason_x005F_x0020_Not_x005F_x0020_Green" to a maxiumum 50 characters with an eplisis on the end. Commented Sep 17, 2009 at 14:59

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't it be $Reason_x005F_x0020_Not_x005F_x0020_Green instead of starting with @?
1

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>

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.