2010-08-24 48 views
0

我遇到了嘗試按日期過濾SharePoint列表的實際問題。我想列出一個月/年的列表,其中每個條目都有一個來自列表的計數,其中計數包括大於或等於該月的第15日或小於下一個月的第15日的日期。我可以在12月份以外的任何月份都做到這一點,我希望計數從2009年12月15日到2010年1月14日(例如)。我真的不明白爲什麼應該有什麼區別,因爲我使用相同的方法。如果任何人都可以在我的代碼中看到錯誤,或者有更好的方法,我會非常感激。Sharepoint日期操作和過濾

<xsl:template name="generateTable"> 
     <xsl:param name="numMonths" /> 
     <xsl:param name="Rows" /> 
     <xsl:param name="dvt_Rows" /> 
     <xsl:param name="tday" select="$startDay" /> 
     <xsl:param name="tmonth" select="($startMonth + msxsl:node-set($numMonths)-1) mod 12 + 1" /> 
     <xsl:param name="tyear" select="$startYear + floor(($numMonths+msxsl:node-set($startMonth)-1) div 12)" /> 
     <xsl:variable name="date" select="concat($tday,'/',$tmonth,'/',$tyear)"/> 

     <tr> 
     <td> 
      <xsl:value-of select="ddwrt:FormatDateTime(string($date),3081,'MMMM yyyy')"/> 
     </td> 
     <td> 
     <xsl:choose> 
     <xsl:when test="$tmonth=12"> 
     <xsl:value-of select="count(/dsQueryResponse/Rows[1]/Row[(
            (ddwrt:FormatDateTime (string (@Date) , 3081, 'M yyyy'))=(ddwrt:FormatDateTime (string ($date) , 3081, 'M yyyy')) 
            and number(substring(ddwrt:FormatDateTime(string(@Date), 3081, 'dd'),1,2)) &gt;=$startDay)])+ 
            count(/dsQueryResponse/Rows[1]/Row[(
       concat(string(number(substring(ddwrt:FormatDateTime (string (@Date) , 3081, 'M yyyy'),1,2))+11),' ', 
       string(number(substring(ddwrt:FormatDateTime (string (@Date) , 3081, 'MM yyyy'),4))-1))=string(ddwrt:FormatDateTime (string ($date) , 3081, 'M yyyy')) 
       and number(substring(ddwrt:FormatDateTime(string(@Date), 3081, 'dd'),1,2)) &lt;$startDay)])"/>  
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="count(/dsQueryResponse/Rows[1]/Row[(
            (ddwrt:FormatDateTime (string (@Date) , 3081, 'M yyyy'))=(ddwrt:FormatDateTime (string ($date) , 3081, 'M yyyy')) 
            and number(substring(ddwrt:FormatDateTime(string(@Date), 3081, 'dd'),1,2)) &gt;=$startDay)])+ 
            count(/dsQueryResponse/Rows[1]/Row[(
       concat(string(number(substring(ddwrt:FormatDateTime (string (@Date) , 3081, 'MM yyyy'),1,2))-1), 
       substring(ddwrt:FormatDateTime (string (@Date) , 3081, 'MM yyyy'),3,6))=(ddwrt:FormatDateTime (string ($date) , 3081, 'M yyyy')) 
       and number(substring(ddwrt:FormatDateTime(string(@Date), 3081, 'dd'),1,2)) &lt;$startDay)])"/> 
     </xsl:otherwise> 
     </xsl:choose> 
     </td> 
     </tr> 

     <xsl:if test="msxsl:node-set($numMonths) &gt; 0"> 
      <xsl:call-template name="generateTable"> 
      <xsl:with-param name="numMonths" select="msxsl:node-set($numMonths)-1" /> 
      <xsl:with-param name="dvt_Rows"/> 
      <xsl:with-param name="Rows"/> 
     </xsl:call-template> 
     </xsl:if> 


</xsl:template> 

在代碼中,tday,tmonth和tyear是在d /月/今天的日期,NUMMONTHS是一個變量來保存在表中所要求的月數,並朝九特派爲15

據我所知,向日期字段添加11個月的工作,但提取年份,將其更改爲一個數字並減去1,就是問題出現的地方。但我無法實現它,所以我完全用完了簡單的想法。

XML文檔是Sharepoint列表,我不知道如何將其作爲純XML共享。我對XML的唯一參考是/ dsQueryResponse/Rows [1]/Row,它選擇Sharepoint列表中的行,並使用Date字段(@Date)執行過濾器。這有幫助嗎?

我使用的是Sharepoint 2007,我通過使用Sharepoint Designer通過插入dataFormWebPart來獲取Sharepoint列表來開始。然後可以使用引用列表中所有行的xpath,/ dsQueryResponse/Rows/Row來訪問XSLT中的Sharepoint列表。 (我在我的代碼中添加了[1]來引用特定的列表,因爲我有兩個列表數據源)。

我寧願首先在Sharepoint中過濾列表,並認爲首先,但也許我做錯了什麼,因爲我無法得到正是我想要的東西 - 其中包括零時沒有條目對於特定的月份,以及月份過濾器的> = 15。我的方法幾乎可行,這就是爲什麼它是如此令人沮喪,但我會歡迎任何替代品,尤其是如果它們更簡單和/或更快。 感謝

+0

您能不能請我們提供XML文檔(儘可能最小化,以便說明問題)?我懷疑可能有更簡單的解決方案,但需要使用XML文檔。 – 2010-08-24 03:37:10

+0

幾個問題..什麼版本的SharePoint?你如何從SharePoint列表中獲取數據?另外,爲什麼使用XSLT過濾SharePoint列表?您不想查詢列表(以過濾行),然後使用XSLT轉換結果嗎? – 2010-08-24 14:03:27

回答

0

作爲例子,這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="kDateByYearAndMonth" match="date" 
      use="translate(substring(.,1,7),'-','') - 
        (15 > substring(.,9,2)) * (1 + (substring(.,6,2)='01') * 88)"/> 
    <xsl:template match="root"> 
     <result> 
      <xsl:apply-templates select="date[count(. | 
               key('kDateByYearAndMonth', 
                translate(substring(.,1,7),'-','') - 
                (15 > substring(.,9,2)) * 
                (1 + (substring(.,6,2)='01') * 88) 
                )[1] 
               ) = 1]"/> 
     </result> 
    </xsl:template> 
    <xsl:template match="date"> 
     <xsl:variable name="vKey" select="translate(substring(.,1,7),'-','') - 
        (15 > substring(.,9,2)) * (1 + (substring(.,6,2)='01') * 88)"/> 
     <xsl:variable name="vDates" select="key('kDateByYearAndMonth',$vKey)"/> 
     <group year="{substring($vKey,1,4)}" month="{substring($vKey,5,2)}" 
       count="{count($vDates)}"> 
      <xsl:copy-of select="$vDates"/> 
     </group> 
    </xsl:template> 
</xsl:stylesheet> 

有了這個輸入:

<root> 
<date>2001-01-01T00:15:00</date> 
<date>2001-01-02T00:15:00</date> 
<date>2001-02-03T00:15:00</date> 
<date>2001-02-04T00:15:00</date> 
<date>2002-03-05T00:15:00</date> 
<date>2002-03-06T00:15:00</date> 
<date>2002-04-07T00:15:00</date> 
<date>2002-04-08T00:15:00</date> 
<date>2003-05-09T00:15:00</date> 
<date>2003-05-10T00:15:00</date> 
<date>2003-06-11T00:15:00</date> 
<date>2003-06-12T00:15:00</date> 
<date>2004-07-13T00:15:00</date> 
<date>2004-07-14T00:15:00</date> 
<date>2004-08-15T00:15:00</date> 
<date>2004-08-16T00:15:00</date> 
<date>2005-09-17T00:15:00</date> 
<date>2005-09-18T00:15:00</date> 
<date>2005-10-19T00:15:00</date> 
<date>2005-10-20T00:15:00</date> 
<date>2006-11-21T00:15:00</date> 
<date>2006-11-22T00:15:00</date> 
<date>2006-12-23T00:15:00</date> 
<date>2006-12-24T00:15:00</date> 
<date>2007-01-25T00:15:00</date> 
<date>2007-01-26T00:15:00</date> 
<date>2007-02-27T00:15:00</date> 
<date>2007-02-28T00:15:00</date> 
<date>2008-03-29T00:15:00</date> 
<date>2008-03-30T00:15:00</date> 
<date>2008-04-31T00:15:00</date> 
</root> 

輸出:

<result> 
    <group year="2000" month="12" count="2"> 
     <date>2001-01-01T00:15:00</date> 
     <date>2001-01-02T00:15:00</date> 
    </group> 
    <group year="2001" month="1" count="2"> 
     <date>2001-02-03T00:15:00</date> 
     <date>2001-02-04T00:15:00</date> 
    </group> 
    <group year="2002" month="2" count="2"> 
     <date>2002-03-05T00:15:00</date> 
     <date>2002-03-06T00:15:00</date> 
    </group> 
    <group year="2002" month="3" count="2"> 
     <date>2002-04-07T00:15:00</date> 
     <date>2002-04-08T00:15:00</date> 
    </group> 
    <group year="2003" month="4" count="2"> 
     <date>2003-05-09T00:15:00</date> 
     <date>2003-05-10T00:15:00</date> 
    </group> 
    <group year="2003" month="5" count="2"> 
     <date>2003-06-11T00:15:00</date> 
     <date>2003-06-12T00:15:00</date> 
    </group> 
    <group year="2004" month="6" count="2"> 
     <date>2004-07-13T00:15:00</date> 
     <date>2004-07-14T00:15:00</date> 
    </group> 
    <group year="2004" month="8" count="2"> 
     <date>2004-08-15T00:15:00</date> 
     <date>2004-08-16T00:15:00</date> 
    </group> 
    <group year="2005" month="9" count="2"> 
     <date>2005-09-17T00:15:00</date> 
     <date>2005-09-18T00:15:00</date> 
    </group> 
    <group year="2005" month="10" count="2"> 
     <date>2005-10-19T00:15:00</date> 
     <date>2005-10-20T00:15:00</date> 
    </group> 
    <group year="2006" month="11" count="2"> 
     <date>2006-11-21T00:15:00</date> 
     <date>2006-11-22T00:15:00</date> 
    </group> 
    <group year="2006" month="12" count="2"> 
     <date>2006-12-23T00:15:00</date> 
     <date>2006-12-24T00:15:00</date> 
    </group> 
    <group year="2007" month="1" count="2"> 
     <date>2007-01-25T00:15:00</date> 
     <date>2007-01-26T00:15:00</date> 
    </group> 
    <group year="2007" month="2" count="2"> 
     <date>2007-02-27T00:15:00</date> 
     <date>2007-02-28T00:15:00</date> 
    </group> 
    <group year="2008" month="3" count="2"> 
     <date>2008-03-29T00:15:00</date> 
     <date>2008-03-30T00:15:00</date> 
    </group> 
    <group year="2008" month="4" count="1"> 
     <date>2008-04-31T00:15:00</date> 
    </group> 
</result> 

注意:這個月的開始日是「硬編碼「,因爲不能有變量引用在xsl:key/@use

Editt:更好的關鍵計算。

+0

謝謝。我用你的只有substring的函數的想法來取代我的冗長的字符串(數字(substring(etc)))函數,似乎工作。不知道爲什麼會這樣。 – AliceA 2010-08-25 04:59:18