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 ,并且我有一个需要translate
的字符串.该字符串是用户输入的文本字段.它包含由定界符分隔的几个较小的字符串. (在这种情况下,它是"|".)此字符串的长度和特殊字符的数量差异很大.
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.)
我需要学习如何将此定界符更改为<br>
.
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!
推荐答案
我需要学习如何将此定界符更改为
<br>
.
以下样式表:
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屋!