XSLT 1.0 转换字符串 - 将字符更改为新行 [英] XSLT 1.0 Translate String - Change Character to New Line
问题描述
我希望这是一个简单的,虽然它似乎经常不是......
I am hoping this is a simple one, though it seems it often isn't...
我正在使用 XLST 1.0,我有一个字符串需要翻译
.此字符串是用户输入的文本字段.它包含由分隔符分隔的几个较小的字符串.(在这种情况下,它是|".)这个字符串的长度和特殊字符的数量差别很大.
I am working with XLST 1.0 and I have a string which I need to translate
. This string is a user-entered text field. It contains several smaller strings separated by a delimiter. (In this case, it's "|".) The length of this string and the number of special characters varies widely.
(此字段类似于 CSV 列表,但不是使用逗号作为分隔符,而是使用|"作为分隔符.)
(This field is similar to a CSV list, however, rather than using a comma as the delimiter, the "|" is the delimiter.)
我需要学习如何将此分隔符更改为
.
I need to learn how to change this delimiter to <br>
.
我已尝试使用以下 XSL 来实现此目的:
I've tried using the following XSL to achieve this:
<xsl:variable name="stringtotrans">
<xsl:text>String1|String2|String3</xsl:text>
</xsl:vairable>
<!-- In the actual XML document, this variable grabs the value of an attribute. -->
<!-- For this example, it's being entered manually. -->
<!-- The number and length of the individual strings varies widely. -->
<xsl:value-of select="translate($stringtotrans, '|', '

')"/>
当这段代码运行时,输出是:
When this code is run, the output is:
String1String2String3
预期/期望的输出是:
String1
String2
String3
对此的任何帮助将不胜感激!
Any and all help with this would be greatly appreciated!
推荐答案
我需要学习如何将此分隔符更改为
.
以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:template match="/">
<xsl:variable name="stringtotrans">
<xsl:text>String1|String2|String3</xsl:text>
</xsl:variable>
<p>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="$stringtotrans"/>
</xsl:call-template>
</p>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:choose>
<xsl:when test="contains($text, $delimiter)">
<xsl:value-of select="substring-before($text, $delimiter)"/>
<br/>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
应用于任何 XML 输入,将返回:
applied to any XML input, will return:
<p>String1<br>String2<br>String3</p>
这篇关于XSLT 1.0 转换字符串 - 将字符更改为新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!