XSLT - 用br标签替换新行

时间:2017-07-26 07:59:46

标签: xslt

有没有办法在XSLT 1.0中用br标签替换新行? 已尝试
 
 &#x0A但仍无法匹配新行。

这是我的代码:

<xsl:template name="HEADLINE">
  <xsl:if test="$gHeadline">
    <xsl:element name="lnv:HEADLINE">
      <xsl:element name="lnvxe:hl1">
        <xsl:variable name="vString">
          <xsl:choose>
            <xsl:when test="contains($gHeadlineCaps,'ENTERT@INMENT.COM')">
              <xsl:variable name="vLeftString" select="substring-before($gHeadline,'@')"/>
              <xsl:variable name="vRightString" select="substring-after($gHeadline,'@')"/>
            <xsl:value-of select="concat($vLeftString,' @',$vRightString)"/> 
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$gHeadline" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="translate($vString,'&#x0A;','&lt;br/&gt;')"/> 
      </xsl:element>
    </xsl:element>
  </xsl:if>
</xsl:template>

这是输入:

 AGONY
 Train derails, dozens hurt in smoky chaos
 Mayor, gov no-shows after latest MTA fail
 EXCLUSIVE Motorman tells News his story

 DON'T CARE
 PREZ 'OK' AS HEALTH BILL FLOPS

 MET LOSS
 PITCHER DEAD AT 51

这是所需的输出:

 AGONY<br/>
 Train derails, dozens hurt in smoky chaos<br/>
 Mayor, gov no-shows after latest MTA fail<br/>
 EXCLUSIVE Motorman tells News his story<br/>
 <br/>
 DON'T CARE<br/>
 PREZ 'OK' AS HEALTH BILL FLOPS<br/>
 <br/>
 MET LOSS<br/>
 PITCHER DEAD AT 51<br/>

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您使用EXSLT中的库,则可以将其写为

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    exclude-result-prefixes="str"
    version="1.0">

    <xsl:import href="http://exslt.org/str/functions/replace/str.replace.template.xsl"/>

    <xsl:template match="text">
        <xsl:copy>
            <xsl:call-template name="str:replace">
                <xsl:with-param name="string" select="."/>
                <xsl:with-param name="search" select="'&#10;'"/>
                <xsl:with-param name="replace"><br/></xsl:with-param>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

并且转换

<text> AGONY
    Train derails, dozens hurt in smoky chaos
    Mayor, gov no-shows after latest MTA fail
    EXCLUSIVE Motorman tells News his story

    DON'T CARE
    PREZ 'OK' AS HEALTH BILL FLOPS

    MET LOSS
    PITCHER DEAD AT 51</text>

<text> AGONY<br/>    Train derails, dozens hurt in smoky chaos<br/>    Mayor, gov no-shows after latest MTA fail<br/>    EXCLUSIVE Motorman tells News his story<br/>    <br/>    DON'T CARE<br/>    PREZ 'OK' AS HEALTH BILL FLOPS<br/>    <br/>    MET LOSS<br/>    PITCHER DEAD AT 51</text>