XSLT1.0用<br/>替换多个出现的char

时间:2016-01-28 23:28:35

标签: xslt-1.0

使用以下与xsl:call-template一起使用的模板,但我需要使用它来将~替换为<br />。我可以在没有HTML替换的情况下使用它,但在我尝试使用<br />&NewLine;&#10;时却无法使用。任何建议:

<xsl:template name="replace-substring">
<xsl:param name="original"/>
<xsl:param name="substring"/>
<xsl:param name="replacement" select="''"/>
<xsl:variable name="first">
    <xsl:choose>
        <xsl:when test="contains($original, $substring)" >
            <xsl:value-of select="substring-before($original, $substring)" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$original"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:variable name="middle">
    <xsl:choose>
        <xsl:when test="contains($original, $substring)">
            <xsl:value-of select="$replacement" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:text></xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:variable name="last">
    <xsl:choose>
        <xsl:when test="contains($original, $substring)">
            <xsl:choose>
                <xsl:when test="contains(substring-after($original, $substring), 
                    $substring)">
                    <xsl:call-template name="replace-substring">
                        <xsl:with-param name="original">
                            <xsl:value-of select="substring-after($original, $substring)"/>
                        </xsl:with-param>
                        <xsl:with-param name="substring">
                            <xsl:value-of select="$substring"/>
                        </xsl:with-param>
                        <xsl:with-param name="replacement">
                            <xsl:value-of select="$replacement"/>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="substring-after($original, $substring)"  />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text></xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($first, $middle, $last)"/>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

很难说出第一个,中间变量和最后一个变量发生了什么,但你应该能够在你的参数中使用文字<br/> ......

<强> XML

<test>one~two~three</test>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="test">
    <xsl:copy>
      <xsl:call-template name="replace-char"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="replace-char">
    <xsl:param name="char" select="'~'"/>
    <xsl:param name="replacement"><br/></xsl:param>
    <xsl:param name="string" select="."/>
    <xsl:variable name="remaining" select="substring-after($string,$char)"/>
    <xsl:value-of select="substring-before(concat($string,$char),$char)"/>
    <xsl:if test="contains($string,$char)">
      <xsl:copy-of select="$replacement"/>      
    </xsl:if>
    <xsl:if test="$remaining">
      <xsl:call-template name="replace-char">
        <xsl:with-param name="string" select="$remaining"/>
        <xsl:with-param name="char" select="$char"/>
        <xsl:with-param name="replacement" select="$replacement"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

<强>输出

<test>one<br/>two<br/>three</test>