Hello there,

I am having issues with the following query:

declare default element namespace "http://www.tracsis.com/TTVP_Interface";

declare variable $date := "2017-07-10";
declare variable $daysRun := 3;
declare variable $diagrams := diagramExchange/unitDiagramList/unitDiagram[@startdate<=$date][@enddate>=$date][substring(@daysrun,$daysRun,1)="Y"];

declare variable $depotConstraint external;
declare variable $resConstraint external;
declare variable $lowerDiagNoConstraint external;
declare variable $upperDiagNoConstraint external;

declare variable $depots := tokenize($depotConstraint," ");
declare variable $resources := tokenize($resConstraint," ");
declare variable $lowerDiagNo := tokenize($lowerDiagNoConstraint," ");
declare variable $upperDiagNo := tokenize($upperDiagNoConstraint," ");

declare function local:begin($diagrams, $index) {
  local:applyDiagStartFilter($diagrams,$index)
};

(: Chain of responsibility pattern used here : once one function exits, passes to the next:)

declare function local:applyDiagStartFilter($diagrams, $index) {
  let $diagNo := $lowerDiagNo[$index]
  return
  if($diagNo = "NIL") then 
  (local:applyDiagEndFilter($diagrams, $index)) 
  else 
  (local:applyDiagEndFilter($diagrams[@id >= $diagNo cast as xs:integer], $index))
};

declare function local:applyDiagEndFilter($diagrams, $index) {
  let $diagNo := $upperDiagNo[$index]
  return
  if($diagNo= "NIL") then 
  (local:applyResFilter($diagrams, $index)) 
  else 
  (local:applyResFilter($diagrams[@id <= $diagNo cast as xs:integer], $index))
};

declare function local:applyResFilter($diagrams, $index) {
  let $res := $resources[$index]
  return
  if($res = "NIL") 
  then 
  (local:applyDepotFilter($diagrams, $index)) 
  else 
  (local:applyDepotFilter($diagrams[res/@id = $res], $index))
};

declare function local:applyDepotFilter($diagrams, $index) {
  let $depot := $depots[$index]
  return
  if($depot = "NIL")
  then 
  ($diagrams) 
  else 
  ($diagrams[depot/@id = $depot])
};

for $depot in $depots
count $count
return local:begin($diagrams, $count)

The underlined sections are the ones giving an issue.

When I set the following like so:

declare variable $depotConstraint := "PM OC";
declare variable $resConstraint := "NIL 180/1";
declare variable $lowerDiagNoConstraint := "NIL NIL";
declare variable $upperDiagNoConstraint := "NIL NIL";

The query runs fine.

When I try and set these externally, I get error "FORG0001 : Cannot cast to xs:double" at the following underlined location:

declare function local:applyDiagStartFilter($diagrams, $index) {
  let $diagNo := $lowerDiagNo[$index]
  return
  if($diagNo = "NIL") then 
  (local:applyDiagEndFilter($diagrams, $index)) 
  else 
  (local:applyDiagEndFilter($diagrams[@id >= $diagNo cast as xs:integer], $index))
};

I do not understand why that should be as there is clearly no mention of xs:double in this query! Also, this happens when $diagNo is "NIL" which is even more perplexing as that would suggest it fails the "if" test somehow.

The $xxxxConstraint variables are all supplied as strings.

What am I doing wrong?

I am using BaseX 8.6.7 in Java 8.

Shaun