开发者

Subtracting duration from dateTime in Cocoa's NSXML XQuery

I have the following XQuery code, which from my reading of the O'Reilly XQuery book seems like it should work:

let $now := current-dateTime()
let $month :=  xs:dayTimeDuration("P30D")
let $month_ago := $now - $month 
return $month_ago

It works fine in this live XQuery in开发者_开发知识库terpreter: http://try.zorba-xquery.com/ But in Cocoa's NSXML it returns the XQuery error "Invalid type for operator."

Is there something I don't understand about Cocoa's implementation of XQuery?


You should use xs:dayTimeDuration or xs:yearMonthDuration for wich are defined substracting operators.

As proof, this XQuery:

let $now := current-dateTime()
let $month :=  xs:dayTimeDuration("P30D")
let $month_ago := $now - $month
return $month_ago

Output:

2011-03-23T14:32:47.156-03:00

Tested on Saxon, Altova, XQSharp.

From http://www.w3.org/TR/xpath20/#mapping, this is the operator 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)`   


This feature is an XPath 2.0 it seems. Maybe your processor doesn't support this?

Executing your query here shows that it is indeed correct. So, with zorba it works...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜