2013-06-04 57 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")]) 
] 
相關問題