2013-06-04 58 views
0

鉴于以下XML,我如何才能在即将到来的生日中找到联系人,例如10天内说的?XPath - 查找即将到来的生日

<contacts> 
    <contact> 
    <name>bob</name> 
    <birthday>1978-05-06</birthday> 
    </contact> 
    <contact> 
    <name>mary</name> 
    <birthday>1955-06-06</birthday> 
    </contact> 
    <contact> 
    <name>john</name> 
    <birthday>1998-05-06</birthday> 
    </contact> 
</contacts> 

我有以下XPath,但它打破了,因为一个月从-日期时间返回一个整数(5),而不是“0”补齐串(05)。有没有办法在Xpath中建立一个日期,它将接受日,月,年的整数参数,而不是我的毫不费力的hack连接字符串?

/contacts/contact[days-from-duration(xs:dateTime(concat(year-from-dateTime(current-date()),'-',month-from-dateTime(xs:dateTime(birthday)),'-',day-from-dateTime(xs:dateTime(birthday)))) - current-date()) < 10] 
+0

下处理左填充,但必须有一个更好的办法:'/联系人/联系/ XS:DATETIME(CONCAT(年 - 从 - 日期时间(目前最新()),' - ',substring(concat('00',month-from-dateTime(xs:dateTime(birthday))),string-length(month-from-dateTime(xs:dateTime(birthday)))+ 1),' - ',substring(concat('00',day-from-dateTime(xs:dateTime(birthday))),string-length(day-from-dateTime(xs:dateTime(birthday))) + 1,2)))' – GGGforce

回答

1

可以使用yearMonthDuration添加这么足年的日期移动到当前年份:

/contacts/contact[ 
    xs:dateTime(birthday) + xs:yearMonthDuration("P1Y") * (year-from-dateTime(current-date()) - year-from-dateTime(xs:dateTime(birthday))) < current-date() + xs:dayTimeDuration("P10D") 
] 

,并检查生日是不是在过去的(像我们到作品避免打字这一切两次在这里):

/contacts/contact[ 
    for $thisYearBirthDay in xs:dateTime(birthday) + xs:yearMonthDuration("P1Y") * (year-from-dateTime(current-date()) - year-from-dateTime(xs:dateTime(birthday))) 
    return $thisYearBirthDay >= current-date() and $thisYearBirthDay < current-date() + xs:dayTimeDuration("P10D") 
] 

然而,实际上使它工作,你需要检查当前和未来年,以防CURR耳鼻喉科日期大约是圣诞节,如:

/contacts/contact[ 
    exists((for $delta in (0, 1) return xs:dateTime(birthday) + xs:yearMonthDuration("P1Y") * ($delta + year-from-dateTime(current-date()) - year-from-dateTime(xs:dateTime(birthday))))[. >= current-date() and . < current-date() + xs:dayTimeDuration("P10D")]) 
] 
相关问题