2010-08-06 83 views
1

我有一些特殊字符的URL,我想替換它們,我使用xslt 1.0,所以我寫的代碼如下。xslt1.0替換不起作用

<xsl:template name="string-replace-all"> 
     <xsl:param name="text"/> 
     <xsl:param name="replace"/> 
     <xsl:param name="by"/> 
     <xsl:choose> 
      <xsl:when test="contains($text,$replace)"> 
       <xsl:value-of select="substring-before($text,$replace)"/> 
       <xsl:value-of select="$by"/> 
       <xsl:call-template name="string-replace-all"> 
        <xsl:with-param name="text" select="substring-after($text,$replace)"/> 
        <xsl:with-param name="replace" select="$replace"/> 
        <xsl:with-param name="by" select="$by"/> 
       </xsl:call-template> 
      </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="$text"/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

,並呼籲像這樣:

<td class="ms-vb"> 
      <xsl:variable name="link"> 
      <xsl:call-template name="string-replace-all"> 
       <xsl:with-param name="text" select="@FileRef"/> 
       <xsl:with-param name="replace" select="'&#39;'"/> 
       <xsl:with-param name="by" select="'%27'"/> 
      </xsl:call-template> 
      </xsl:variable>--> 
       <a target="_blank" href="@link" ><xsl:value-of select="@New_x0020_Doc_x0020_Title" disable-output-escaping="yes" /></a> 
      </td> 

我收到錯誤 「Web部件無法顯示」。任何人都可以請建議我在這裏做錯了什麼?

+0

好問題(+1)。請參閱我的答案以獲得解釋和完整解 – 2010-08-06 18:41:26

回答

2

提供的「替換」模板工作

爲了證實這一點使用下面的轉換:

<t FileRef="Abc'Xyz'Tuv"/> 

想要的,正確的結果產生

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="/*"> 
     <xsl:variable name="link"> 
      <xsl:call-template name="string-replace-all"> 
       <xsl:with-param name="text" select="@FileRef"/> 
       <xsl:with-param name="replace">'</xsl:with-param> 
       <xsl:with-param name="by" select="'%27'"/> 
      </xsl:call-template> 
     </xsl:variable> 

     "<xsl:value-of select="$link"/>" 
    </xsl:template> 

    <xsl:template name="string-replace-all"> 
     <xsl:param name="text"/> 
     <xsl:param name="replace"/> 
     <xsl:param name="by"/> 
     <xsl:choose> 
      <xsl:when test="contains($text,$replace)"> 
       <xsl:value-of select="substring-before($text,$replace)"/> 
       <xsl:value-of select="$by"/> 
       <xsl:call-template name="string-replace-all"> 
        <xsl:with-param name="text" select="substring-after($text,$replace)"/> 
        <xsl:with-param name="replace" select="$replace"/> 
        <xsl:with-param name="by" select="$by"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$text"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

當這個XML文檔上應用的轉換

"Abc%27Xyz%27Tuv" 

的問題可能出在你的方式指定replace參數(式中奇數撇號):

而不是

<xsl:with-param name="replace" select="'&#39;'"/> 

使用

<xsl:with-param name="replace">'</xsl:with-param> 
+0

+1好抓!此外,這將工作:'' – 2010-08-06 18:49:19

+0

其實我們發現問題是與共享點。一旦用戶重命名文件一切正常。謝謝回覆。我希望我稍後再使用它。 – user346514 2010-08-10 20:19:16

+0

@ user346514:你的評論是錯誤的。這個聲明''產生這個錯誤:'一個字符串文字沒有關閉。 – 2010-08-10 23:23:31