XSL 1.0,如何在注意不分割单词的情况下分割字符串
XSL 1.0, How to split string with taking care about not slicing words
我必须改进 XSL 中的长字符串拆分。行大小为 60 个字符。当出现相当长的字符串时,它以如此不优雅的方式分成几行。
我尝试实现照顾 spaces 的机制,以避免在它们中间切词。
现在,代码如下所示:
<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版本,但升级它并非不可能,所以我必须使用我现有的。
我害怕依赖于每一行中最后出现的 space 字符,因为输入可以是一个没有任何 space 的长字符序列,那么最好的选择仍然是使用我粘贴的代码。
在 XSL 中是否有一些简单的方法来标记化?
只要摘要长度小于行容量,我是否应该标记完整字符串并附加每个下一个标记?
或者我应该检查行中的最后一个字符是否是 space 字符,然后再进行一些额外的操作?
我很困惑,这是我第一次与 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 on the 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 的标记化,并开始遍历标记列表,在每个循环后检查行长度。
我必须改进 XSL 中的长字符串拆分。行大小为 60 个字符。当出现相当长的字符串时,它以如此不优雅的方式分成几行。 我尝试实现照顾 spaces 的机制,以避免在它们中间切词。
现在,代码如下所示:
<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版本,但升级它并非不可能,所以我必须使用我现有的。
我害怕依赖于每一行中最后出现的 space 字符,因为输入可以是一个没有任何 space 的长字符序列,那么最好的选择仍然是使用我粘贴的代码。 在 XSL 中是否有一些简单的方法来标记化?
只要摘要长度小于行容量,我是否应该标记完整字符串并附加每个下一个标记? 或者我应该检查行中的最后一个字符是否是 space 字符,然后再进行一些额外的操作?
我很困惑,这是我第一次与 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 on the 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 的标记化,并开始遍历标记列表,在每个循环后检查行长度。