 <p>There are multiple syntax errors in your query:</p> <ul> <li><code>let</code> clauses have to be part of a <em>FLWOR</em> expression, which always ends with a <code>return</code> clause.</li> <li><code>if</code> cannot be used without <code>then</code> and <code>else</code> and does not use curly braces.</li> <li>The opening tag <code>&lt;res&gt;</code> needs a matching closing tag <code>&lt;/res&gt;</code> at the end of the query.</li> </ul> <p>The corrected query looks like this:</p> <pre><code>declare variable $datesList := ( "2013-01-01-00.30.00", "2013-01-01-01.00.00", "2013-01-01-01.30.00", "2013-01-01-02.00.00", "2013-01-01-02.30.00", "2013-01-01-03.00.00", "2013-01-01-03.30.00", "2013-01-01-04.00.00" ); &lt;res&gt;{ let $mcId1 := count(//ZZQAD2UsageTransactionSVC/usagePeriods/usagePeriodsList/SQs/SQsList[1]/mL) let $mcId2 := count(//ZZQAD2UsageTransactionSVC/usagePeriods/usagePeriodsList/SQs/SQsList[2]/mL) return if($mcId1 = 8) then ( for $mlList in //ZZQAD2UsageTransactionSVC/usagePeriods/usagePeriodsList/SQs/SQsList[1]/intervals/mL return if($mcId1 &gt; $mcId2) then &lt;text&gt;true&lt;/text&gt; else &lt;text&gt;false&lt;/text&gt; ) else () }&lt;/res&gt; </code></pre>