使用 XQuery 过滤当前周的值 [英] Filter Values of Current Week with XQuery
问题描述
我正在使用 XQuery 从 Sharepoint 提取数据.xquery 代码是:
I am extracting data from Sharepoint using XQuery. The xquery code is:
{for $ancestor0 in $queryresponse//*:row
return<Row>
<Title>{fn:data($ancestor0/@ows_Title)}</Title>
<EventDate>{fn:data($ancestor0/@ows_EventDate)}</EventDate>
返回的数据为:
<h1>Meeting Agenda Event Date</h1>
New England APCD Work Group 6/26/2013
NY APD Workgroup 6/27/2013
MCDB 2012 Data Submission Bi-weekly 7/2/2013
MA APCD 7/3/2013
VA APCD Monthly Status 7/4/2013
Cross State Meeting 7/5/2013
NY APD Workgroup 7/11/2013
MA APCD 7/17/2013
Cross State Meeting 7/19/2013
NY APD Workgroup 7/25/2013
MA APCD 7/31/2013
New England APCD Work Group 7/31/2013
VA APCD Monthly Status 8/1/2013
Cross State Meeting 8/2/2013
我需要获取当前周的行吗?所以我只会得到以下行作为结果:
I need to get the rows that are in the current-week? so I would the get only the following rows as results:
MA APCD 7/17/2013
Cross State Meeting 7/19/2013
有人可以帮我写代码吗?
Can someone help me on the code?
推荐答案
最后你需要解决两个问题.一个是解析这些日期,另一个是验证它们是否在当前周.
In the end you need to solve two problems. One is parsing these dates, the other verifying if they're in the current week.
测试某一天是否在当前周只是某一天是否在给定日期的同一周(即当前这一天)的特殊情况.
Testing whether a day is in the current week is only a special case of whether a day is in the same week of a given day, namely the current day one's.
declare function local:in-current-week($date as xs:date)
as xs:boolean
{
local:in-same-week(current-date(), $date)
};
要测试某一天是否在给定的一周内,您需要找到一周的开始和结束时间.如果您知道星期几,则很容易找到一周的开始;并且可以通过从已知的一周开始计算天数来找到这一点(任意选择,让我们选择 1912 年 6 月 23 日星期日)并使用除以一周长度的余数.
For testing whether a day is in a given week, you need to find start and end of week. Finding the start of week is easy if you know the day of week; and finding this can be done by counting the days from a known start of week (choose arbitrarily, lets go for Sunday June 23rd, 1912) and use the remainder of dividing by a week's length.
如果您希望从星期一开始数周,只需将日期更改为一个,我会选择 1938 年 1 月 10 日.
If you want weeks to start at a Monday, just change the date to one, I'd go for January 10th, 1938.
declare function local:in-same-week($date1 as xs:date, $date2 as xs:date)
as xs:boolean
{
let $dayOfMonth := abs(($date1 - xs:date('1912-06-23')) div xs:dayTimeDuration('P1D') mod 7)
let $startOfWeek := $date1 - $dayOfMonth * xs:dayTimeDuration('P1D')
let $endOfWeek := $startOfWeek + 7 * xs:dayTimeDuration('P1D')
return $startOfWeek <= $date2 and $date2 < $endOfWeek
};
local:in-current-week(xs:date('2013-07-17'))
根据美国日期格式构建 xs:date
这只是一个简单的正则表达式(适用于所有日期 AC.):
Constructing an xs:date
From US Date Format
This does not involve anything more than a simple regular expression (valid for all dates AC.):
xs:date(replace('07/10/2013', '(\d{1,2})/(\d{1,2})/(\d+)', '$3-$1-$2'))
<小时>
最后,您可以通过将这个 where
子句添加到您的查询中来进行过滤:
Finally you can filter by adding this where
clause to your query:
where local:in-current-week(xs:date(replace(@ows_Title, '(\d{1,2})/(\d{1,2})/(\d{4})', '$3-$1-$2')))
这篇关于使用 XQuery 过滤当前周的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!