Search code examples
xmlxquerymarklogic

XQuery Unexpected if error during input processing


I'm trying to design a simple application which utilizes MarkLogic Query Console and the MarkLogic database.

My code looks like this:

declare namespace link="http://www.xbrl.org/2003/linkbase";
declare namespace bd-alg="http://www.nltaxonomie.nl/nt11/bd/20161207/dictionary/bd-algemeen";
declare namespace bd-bedr="http://www.nltaxonomie.nl/nt11/bd/20161207/dictionary/bd-bedrijven";
declare namespace bd-bedr-tuple="http://www.nltaxonomie.nl/nt11/bd/20161207/dictionary/bd-bedr-tuples";
declare namespace bd-dim-mem="http://www.nltaxonomie.nl/nt11/bd/20161207/dictionary/bd-domain-members";
declare namespace bd-dim-dim="http://www.nltaxonomie.nl/nt11/bd/20161207/validation/bd-axes";
declare namespace xbrldi="http://xbrl.org/2006/xbrldi";
declare namespace xbrli="http://www.xbrl.org/2003/instance";
declare namespace iso4217="http://www.xbrl.org/2003/iso4217";
declare namespace xlink="http://www.w3.org/1999/xlink";

let $startDateInput := ""
let $endDateInput := ""

if($startDateInput)
  {
    then let $startDate := xs:date($startDateInput)
    else let $startDate := xs:date("1900-01-01")
  }
  
if($endDateInput)
  {
    then let $endDate := xs:date($endDateInput)
    else let $endDate := xs:date("2100-12-31")
  }
    
for $doc in /xbrli:xbrl
    let $docId := $doc/xbrli:context//xbrli:identifier/text()
    let $docStartDate := xs:date($doc//xbrli:startDate/text())
    let $docEndDate := xs:date($doc//xbrli:endDate/text())
    where $docStartDate >= $startDate and $docEndDate <= $endDate    
    order by $docStartDate, $docId + 1  
  return 
  (
    $docId,
    $docStartDate,
    $docEndDate
  )

The error I'm getting is the Unexpected If Error on this operation

if($startDateInput)
  {
    then let $startDate := xs:date($startDateInput)
    else let $startDate := xs:date("1900-01-01")
  }

My guess is that the second if will give the same error so lets keep it at this one.

Do any of you guys understand what I am doing wrong.

I've tried placing a comma and semi-colon. Those give me other errors, so that's not the problem.

Thanks in advance!


Solution

  • You need to rewrite your code. if is without curly braces, but you are also interrupting the logic of the FLWOR statement. Keep in mind XQuery is a functional language. Do something like this:

    let $startDateInput := ""
    let $endDateInput := ""
    
    let $startDate :=
        if($startDateInput)
        then xs:date($startDateInput)
        else xs:date("1900-01-01")
    
    let $endDate :=
        if($endDateInput)
        then xs:date($endDateInput)
        else xs:date("2100-12-31")
    
    for $doc in /xbrli:xbrl
    ...
    

    HTH!