2015-05-20 80 views
3

如何更換一個XML值,例如:通過XSLT如何更換 「單引號」,以 「雙單引號」 在XSLT

<name>Linda O''Connel</name> 

<name>Linda O'Connel</name> 

什麼?

我需要這個,因爲我必須在powershell命令行和其他平臺上傳遞此值,因爲需要「雙單引號」來轉義撇號/單引號。

+2

你意識到PowerShell只能讀取XML內容,你可以用這種方式工作嗎?而不是試圖將其轉換爲字符串,然後轉義字符串。 – TheMadTechnician

+0

我絕對可以做到這一點,但我需要通過「雙單引號」到其他平臺以及這是他們如何逃脫單引號。那麼,如果有什麼辦法可以通過XSLT來完成?謝謝 – macjop

回答

8

假設一個XSLT 1.0處理器,則需要使用遞歸命名模板爲此,e.g:通話

<xsl:template name="replace"> 
    <xsl:param name="text"/> 
    <xsl:param name="searchString">'</xsl:param> 
    <xsl:param name="replaceString">''</xsl:param> 
    <xsl:choose> 
     <xsl:when test="contains($text,$searchString)"> 
      <xsl:value-of select="substring-before($text,$searchString)"/> 
      <xsl:value-of select="$replaceString"/> 
      <!-- recursive call --> 
      <xsl:call-template name="replace"> 
       <xsl:with-param name="text" select="substring-after($text,$searchString)"/> 
       <xsl:with-param name="searchString" select="$searchString"/> 
       <xsl:with-param name="replaceString" select="$replaceString"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

例子:

<xsl:template match="name"> 
    <xsl:copy> 
     <xsl:call-template name="replace"> 
      <xsl:with-param name="text" select="."/> 
     </xsl:call-template> 
    </xsl:copy> 
</xsl:template> 
-2

您也可以嘗試以下。

<xsl:variable name="temp">'</xsl:variable> 
    <name> 
    <xsl:value-of select="concat(substring-before(name,$temp),$temp,$temp,substring-after(name,$temp))"/> 
    </name> 
+1

你假設只有一個撇號。 –