用XSLT 转HTML中插入换行符的解决办法

本质问题是需要吧xslt中代表换行的&#xA; 用html中的<br/>来代替。

下面是抄来的解决方案

<xsl:template name="lf2br">
  <!-- import $StringToTransform -->
  <xsl:param name="StringToTransform"/>
  <xsl:choose>
    <!-- string contains linefeed -->
    <xsl:when test="contains($StringToTransform,'&#xA;')">
      <!-- output substring that comes before the first linefeed -->
      <!-- note: use of substring-before() function means        -->
      <!-- $StringToTransform will be treated as a string,       -->
      <!-- even if it is a node-set or result tree fragment.     -->
      <!-- So hopefully $StringToTransform is really a string!   -->
      <xsl:value-of select="substring-before($StringToTransform,'&#xA;')"/>
      <!-- by putting a 'br' element in the result tree instead  -->
      <!-- of the linefeed character, a
<br>
will be output at   -->
              <!-- that point in the HTML                                -->
              <br/>
              <!-- repeat for the remainder of the original string -->
              <xsl:call-template name="lf2br">
                <xsl:with-param name="StringToTransform">
                  <xsl:value-of select="substring-after($StringToTransform,'&#xA;')"/>
                </xsl:with-param>
              </xsl:call-template>
          </xsl:when>
          <!-- string does not contain newline, so just output it -->
          <xsl:otherwise>
            <xsl:value-of select="$StringToTransform"/>
          </xsl:otherwise>
  </xsl:choose>
</xsl:template>

 

<xsl:template name="CopyWithLineBreakBR">
  <xsl:param name="string"/>
  <xsl:variable name="Result">
    <xsl:call−template name="lf2br">
      <xsl:with−param name="StringToTransform" select="$string"/>
    </xsl:call−template>
  </xsl:variable>
  <xsl:copy−of select="$Result"/>
</xsl:template>

使用时

<xsl:call−template name="CopyWithLineBreakBR">
  <xsl:with−param name="string" select="'my breaked
string'"/>
</xsl:call−template>

 

Leave a Reply