XSL 1.0,如何在不切分单词的情况下分割字符串

问题描述

我必须改进XSL中拆分长字符串的过程。行大小为60个字符。当出现很长的字符串时,它以非常微妙的方式拆分为行。 我尝试实现一种照顾空间的机制,以避免在中间切词。

现在的代码如下:

<xsl:template name="split_text">    
       <xsl:param name="sText"/>
       <xsl:param name="linesize">60</xsl:param>
    
       <xsl:variable name="todisplay" saxon:assignable="yes"/>
       <xsl:variable name="toProcess" saxon:assignable="yes" select="$sText"/>

       <saxon:while test="string-length($toProcess) > $linesize">
          <saxon:assign name="todisplay" select="substring($toProcess,1,$linesize)"/>
          <saxon:assign name="toProcess" select="substring($toProcess,$linesize + 1)"/>
          <xsl:value-of select="$todisplay"/><br/>
       </saxon:while>
       <xsl:value-of select="$toProcess"/>

    </xsl:template>

如果长度超过行容量,则仅拆分文本。 我想处理行容量在某些单词中间结束的情况。我读到了关于分词器,substring-before-last等的信息。但是我在Java中有一些例外。可能我正在使用过旧的XSL版本,但是并非不可能升级,所以我必须使用现有的。

我担心依赖于每行最后出现的空格字符,因为输入可以是没有任何空格的长字符序列,因此最佳选择仍然是使用我粘贴的代码。 用XSL以简单的方式标记化吗?

如果总长度小于行容量,我应该对完整字符串进行标记并附加每个下一个标记吗? 还是应该检查行中的最后一个字符是否为空格字符,然后再进行一些其他操作?

我很困惑,这是我第一次与XSL约会。

其他编辑: 我发现功能saxon:tokenize对我来说很有趣。文档中的描述听起来很棒-这就是我所需要的。但是可以在XSL 1.0和Saxon中使用-从Manifest中粘贴:

Manifest-Version: 1.0
Main-Class: com.icl.saxon.StyleSheet
Created-By: 1.3.1_16 (Sun Microsystems Inc.)
```.

If yes,how to iterate over that? I found in web some varIoUs styles of iterating and I don't kNow,and don't understand what differences,pros and cons are between they

解决方法

好的,我已经完成了,所以我将分享我的解决方案,也许有人会有类似的问题。

<xsl:template name="split_text">    
       <xsl:param name="sText"/>
       <xsl:param name="lineSize">60</xsl:param>
    
       <xsl:variable name="remainder" saxon:assignable="yes"/>
       <xsl:variable name="textTokens" saxon:assignable="yes" select="saxon:tokenize($sText)" />

            <xsl:choose>
            <!-- If line length is fill,then it is printed and remainder is cleared -->
                <xsl:when test="(string-length($remainder) >= $lineSize)">
                    <xsl:value-of select="$remainder"/><br/>
                    <saxon:assign name="remainder" select="''"/>                
                </xsl:when>
                <!-- Words are sequentially adding to line until it become filled -->
                <xsl:otherwise>
                    <saxon:assign name="remainder" select="concat($remainder,' ',$currentToken,' ')"/>
                </xsl:otherwise>
            </xsl:choose>           
        </xsl:for-each>
    </xsl:template>

我使用了saxon的标记化,并开始遍历标记列表,在每次循环后检查行长。