代码之家  ›  专栏  ›  技术社区  ›  toomanyairmiles

在SharePoint中截断文本字符串

  •  1
  • toomanyairmiles  · 技术社区  · 17 年前

    我正在使用此脚本截断SharePoint2007中的文本字符串,但它不起作用,我不明白为什么?!非常感谢您提出的任何想法。

    从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>
    

    从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 回复  |  直到 14 年前
        1
  •  1
  •   Thiyagaraj    17 年前

    从我的初始外观来看,“50”似乎不是作为字符串发送的,用单引号将其括起来。

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

    或者因为它是一个数字,所以显式地将其强制转换为数字。

    <xsl:with-param name="allowablelength" select="number(50)"></xsl:with-param>
    
        2
  •  3
  •   Welbog    17 年前

    好吧,首先,你用的嵌套都错了。你的 param with-param 元素不应该以这种方式嵌套。将您拥有的替换为:

    <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>
    

    <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>
    

    注意 string-length 有连字符。

        3
  •  1
  •   Wayferer    14 年前

    我知道这是一条古老的线索,但它让我开始解决一个问题,我想我将来会把我的结果放在这里给别人看。

    我们正在使用SharePoint2010企业搜索,对于结果页面,我要求从中心缩短URL,并包括突出显示。但是,当URL被缩短时,突出显示不起作用,可能有一种更简单/更好的方法可以做到这一点,但我做到了:

    <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>