XSLT 1.0 Translate String - Change Character to New Line

元气小坏坏 提交于 2019-12-17 17:15:56

问题


I am hoping this is a simple one, though it seems it often isn't...

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.

(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>.

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, '|', '&#xA;&#xD;')"/>

When this code is run, the output is:

String1String2String3

The expected/desired output is:

String1
String2
String3

Any and all help with this would be greatly appreciated!


回答1:


I need to learn how to change this delimiter to <br>.

The following stylesheet:

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>

applied to any XML input, will return:

<p>String1<br>String2<br>String3</p>



回答2:


The translate() function maps from character to character. In your case, you're replacing | with &#xA; (the first character), which is line feed (LF). This may work on Unix systems where LF commonly marks end of line, but it won't work on Windows, for example, where the end of line marker is CR+LF (&#xD;&#xA;).

For further details on EOLs in XML, see How to add a newline (line break) in XML file?

For replacing a character with a string in XSLT 1.0, see Michael's recursive template for replace.



来源:https://stackoverflow.com/questions/35855412/xslt-1-0-translate-string-change-character-to-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!