2016-11-07 88 views
0

我想我會在西班牙語StackOverflow上回答question。這個問題涉及使用遞歸模板來處理XSLT 1.0中的字符串替換。我可以通過將參數參數括在單引號中來消除錯誤消息,但是我仍然得到空白的輸出結果。XSLT 1.0遞歸字符串替換函數返回空文檔

這裏是XML:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <URL>[sitio]/PublishingImages/[nombreimagen].jpg</URL> 
</document> 

下面是所需的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <URL>[sitio]/PublishingImages/_t/[nombreimagen]_jpg.jpg</URL> 
</document> 

這裏是XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="URL"> 
     <xsl:variable name="ImageUrl"><xsl:value-of select="URL"/></xsl:variable> 
    <xsl:variable name="primerreplace"> 
     <xsl:call-template name="string-replace"> 
      <xsl:with-param name="text" select="$ImageUrl" /> 
      <xsl:with-param name="pattern" select="'PublishingImages'" /> 
      <xsl:with-param name="replace-with" select="'PublishingImages/_t'" /> 
     </xsl:call-template> 
    </xsl:variable>        
    <xsl:variable name="segundoreplace"> 
     <xsl:call-template name="string-replace"> 
      <xsl:with-param name="text" select="$primerreplace" /> 
      <xsl:with-param name="pattern" select="'.jpg'" /> 
      <xsl:with-param name="replace-with" select="'_jpg.jpg'" /> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:value-of select="$segundoreplace" /> 
    </xsl:template> 

</xsl:stylesheet> 

回答

0

您需要更改的ImageUrl變量聲明由此...

<xsl:variable name="ImageUrl"><xsl:value-of select="URL"/></xsl:variable> 

要這個......

<xsl:variable name="ImageUrl"><xsl:value-of select="."/></xsl:variable> 

或者更好的,只是這...

<xsl:variable name="ImageUrl" select="." /> 

這是因爲你在匹配URL模板,這樣做<xsl:value-of select="URL" />正在尋找當前URL元素稱爲URL的子元素。

+0

謝謝......我幾乎擁有它! – b00kgrrl