2013-12-12 14 views
0

我有一個字符串,可能有換行符和撇號。我需要替換兩者。我有以下XSL代碼:XSL 1.0如何替換字符串中的兩個不同的東西

<xsl:call-template name="replaceapostrophes"> 
<xsl:with-param name="string"> 
    <xsl:call-template name="replacelinefeeds"> 
     <xsl:with-param name="string" select="hl7:text/hl7:paragraph"/> 
    </xsl:call-template> 
</xsl:with-param> 
</xsl:call-template> 

    <!-- template for replacing line feeds with <br> tags for page display --> 
<xsl:template name="replacelinefeeds"> 
    <xsl:param name="string"/> 
    <xsl:choose> 
     <xsl:when test="contains($string,'&#10;')"> 
      <xsl:value-of select="substring-before($string,'&#10;')"/> 
      <br/> 
      <xsl:call-template name="replacelinefeeds"> 
       <xsl:with-param name="string" select="substring-after($string,'&#10;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$string"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<!-- template for replacing html encoded apostrophes for page display --> 
<xsl:template name="replaceapostrophes"> 
    <xsl:param name="string"/> 
    <xsl:choose> 
     <xsl:when test="contains($string, '&amp;#39;')"> 
      <xsl:value-of select="substring-before($string,'&amp;#39;')"/>'<xsl:call-template name="replaceapostrophes"> 
       <xsl:with-param name="string" select="substring-after($string,'&amp;#39;')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$string"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

這是XML代碼:

<text> 
    <paragraph>Adding apostrophe to the patient&amp;#39;s instructions 
    and checking for a second line</paragraph> 
</text> 

然而,當這個運行時,我發現了撇號佔了,但不換行。

Adding apostrophe to the patient's instructions and checking for a second line 

而不是

Adding apostrophe to the patient's instructions 
and checking for a second line 

它正常工作,如果有一個在相同的字符串一個或另一個但不是兩者。

有沒有不同的方式,我需要做到這些?

謝謝

回答

1

試着用相反的方法(首先替換撇號,然後換行)。

基本上,您將一個HTML <br/>元素放入您的變量中,然後以其文本值替換撇號,從而再次刪除換行符。

+0

切換它們,它工作正常。謝謝 – jjasper0729

1

反過來使用模板,即首先替換撇號,然後換行。並且確保在您輸出時使用xsl:copy-of而不是xsl:value-of替換換行的結果,否則br元素將會丟失。所以如果你有<xsl:variable name="text"><xsl:call-template name="replacelinefeeds">..</xsl:call-template></xsl:variable>,請確保你使用<xsl:copy-of select="$text"/>

相關問題