2013-12-12 16 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"/>

相关问题