如何获取字符串中char的最后一个索引?

svz*_*svz 3 xslt indexof xslt-1.0

我有以下任务:
有一个包含长字符串的xml元素.我需要使用xsl将此元素转换为许多html <input>标记.它的工作方式如下:如果字符串长度超过<input>字段可以保持而不滚动我递归调用相同的模板以创建另一个带有remainingig文本的输入字段.

问题是字符串经常在单词的中间分割,这是不好的.

所以我需要找到最后一个space字符的位置,该位置不大于适合<input>标记的子字符串的大小,并且只打印行前的子字符串.

所以我准备了一个最大长度的子字符串,它可以适合字段,但我不知道如何获取其中的最后一个索引,space并将其作为参数传递给函数的下一个调用.

UPD:这是我到目前为止所拥有的

<xsl:template name="multilineInput">
    <xsl:param name="input" select="."/>
    <xsl:param name="maxFirst" select="."/>
    <xsl:param name="firstLineWidth" select="."/>



    <input>
        <xsl:attribute name="readonly">readonly</xsl:attribute>
        <xsl:attribute name="class">input_multiline</xsl:attribute>
        <xsl:attribute name="style">width = "<xsl:value-of select="$firstLineWidth"/>"</xsl:attribute>
        <xsl:attribute name="type">text</xsl:attribute>
        <xsl:attribute name="value"><xsl:value-of select="substring($input, 1, $maxFirst)"/></xsl:attribute>
    </input>

    <xsl:if test="$maxFirst &lt; string-length($input)">
        <xsl:call-template name="multilineInput">
            <xsl:with-param name="input" select="substring($input, $maxFirst+1, string-length($input)-$maxFirst)"/>
            <xsl:with-param name="maxFirst" select="110"/>
            <xsl:with-param name="firstLineWidth" select="'980'"/>
        </xsl:call-template>
    </xsl:if>

</xsl:template>
Run Code Online (Sandbox Code Playgroud)

Mad*_*sen 6

以下递归模板可用于返回给定分隔符的last-index:

<xsl:template name="last-index-of">
    <xsl:param name="txt"/>
    <xsl:param name="remainder" select="$txt"/>
    <xsl:param name="delimiter" select="' '"/>

    <xsl:choose>
        <xsl:when test="contains($remainder, $delimiter)">
            <xsl:call-template name="last-index-of">
                <xsl:with-param name="txt" select="$txt"/>
                <xsl:with-param name="remainder" select="substring-after($remainder, $delimiter)"/>
                <xsl:with-param name="delimiter" select="$delimiter"/>
            </xsl:call-template>      
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="lastIndex" select="string-length(substring($txt, 1, string-length($txt)-string-length($remainder)))+1"/>
            <xsl:choose>
                <xsl:when test="string-length($remainder)=0">
                    <xsl:value-of select="string-length($txt)"/>
                </xsl:when>
                <xsl:when test="$lastIndex>0">
                    <xsl:value-of select="($lastIndex - string-length($delimiter))"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="0"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

它可以像这样调用:

<xsl:call-template name="last-index-of">
    <xsl:with-param name="txt" select="'The quick brown fox jumped over the lazy dog.'"/>
    <xsl:with-param name="delimiter" select="' '"></xsl:with-param>
 </xsl:call-template>
Run Code Online (Sandbox Code Playgroud)

并返回: 41

您可以将模板调用的结果分配给如下变量:

<xsl:variable name="last-index">
    <xsl:call-template name="last-index-of">
        <xsl:with-param name="txt" select="'The quick brown fox jumped over the lazy dog.'"/>
        <xsl:with-param name="delimiter" select="' '"></xsl:with-param>
     </xsl:call-template>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)