2017-07-26 80 views
0

有沒有辦法在XSLT 1.0中用br標記替換新行? 嘗試

&#x0A但仍不能匹配新行。XSLT - 用br標記替換新行

這裏是我的代碼:

<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,'[email protected]')"> 
       <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/> 

謝謝!

+0

的輸出是什麼方法,'html', 'xml',普通的'text'? –

+0

輸出是xml –

+0

現在有一些解決方案,例如http://exslt.org/str/functions/replace/str.replace.template.xsl,在http://exslt.org/str/functions/上解釋replace/index.html –

回答

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> 
+0

有沒有辦法做到這一點,而無需導入模板? –

+0

好的,是的,將引用文件中的代碼複製到樣式表中,或者在引用的代碼中編寫類似於自己的模板。 –