2016-11-14 49 views
0

我想在xslt2中添加兩個變量。該想法是下個月(如果是上個月,可能會增加一年)。net.sf.saxon.trans.XPathException:算術運算符沒有爲類型參數(xs:string,xs:string)定義

我使用的代碼如下:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" exclude-result-prefixes="xsl xs fn xdt"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:param name="filter_date"/> 

    <xsl:variable name="Current_Date"> 
     <xsl:choose> 
      <xsl:when test="$filter_date"> 
       <xsl:value-of select="$filter_date"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="format-date(current-date(), '[Y0001][M01][D01]')"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <xsl:variable name="Current_Month" select="substring($Current_Date,5,2)"/> 
    <xsl:variable name="Current_Year" select="substring($Current_Date,1,4)"/> 
    <xsl:variable name="One">1</xsl:variable> 

    <xsl:variable name="Month"> 
     <xsl:choose> 
      <xsl:when test="$Current_Month = '12'"> 
       <xsl:text>01</xsl:text> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$Current_Month + '0' + $One"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <xsl:variable name="Year"> 
     <xsl:choose> 
      <xsl:when test="$Current_Month = '12'"> 
       <xsl:value-of select="sum($Current_Year + $One)"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$Current_Year"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 

    <out> 
     <xsl:value-of select="concat($Year, $Month, '20')" /> 
    </out> 

</xsl:template> 

但我得到一個錯誤

net.sf.saxon.trans.XPathException:算術運算符無定義類型參數(xs:string,xs:string)。

回答

0

如果你想要做的日期計算在XSLT/XPath 2.0中,你可以簡單的持續時間增加的日期,例如與+一個月添加到當前日期使用

<xsl:value-of select="current-date() + xs:yearMonthDuration('P1M')"/> 

添加字符串在XSLT/XPath中不受支持,我甚至不確定是否期望執行字符串連接或將操作數先轉換爲數字。

+0

感謝您的迴應和幫助。該功能有助於:)祝你有美好的一天。 – Vicky

相關問題