您应该使用xs:dayTimeDuration
orxs:yearMonthDuration
来定义减法运算符。
作为证明,这个 XQuery:
let $now := current-dateTime()
let $month := xs:dayTimeDuration("P30D")
let $month_ago := $now - $month
return $month_ago
输出:
2011-03-23T14:32:47.156-03:00
在 Saxon、Altova、XQSharp 上测试。
来自http://www.w3.org/TR/xpath20/#mapping,这是运算符映射:
Operator | Type(A) | Type(B) | Function
A - B | numeric | numeric | op:numeric-subtract(A, B)
A - B | xs:date | xs:date | op:subtract-dates(A, B)
A - B | xs:date | xs:yearMonthDuration | op:subtract-yearMonthDuration-from-date(A, B)
A - B | xs:date | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-date(A, B)
A - B | xs:time | xs:time | op:subtract-times(A, B)
A - B | xs:time | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-time(A, B)
A - B | xs:dateTime | xs:dateTime | op:subtract-dateTimes(A, B)
A - B | xs:dateTime | xs:yearMonthDuration | op:subtract-yearMonthDuration-from-dateTime(A, B)
A - B | xs:dateTime | xs:dayTimeDuration | op:subtract-dayTimeDuration-from-dateTime(A, B)
A - B | xs:yearMonthDuration | xs:yearMonthDuration | op:subtract-yearMonthDurations(A, B)`