2

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 interpreter: 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?

Gabriel Roth
  • 1,030
  • 1
  • 12
  • 31

2 Answers2

3

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)`   
1

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...

Dennis Münkle
  • 5,036
  • 1
  • 19
  • 18
  • I'm using Cocoa's NSXML, which according to the documentation "includes support for XQuery 1.0 and XPath 2.0." So maybe this is a problem with NSXML's XPath implementation? I've edited the question with this information. – Gabriel Roth Apr 22 '11 at 17:07