Error in XQuery
Added by Denis Sukhoroslov about 8 years ago
Saxon 9.7.0-10
The following query:
@@ declare namespace http = "http://www.expath.org/http"; declare namespace rest = "http://www.expath.org/restxq"; declare namespace p = "http://hl7.org/fhir"; declare variable $id external;
let $itr := collection("Patients")/p:Patient[p:id/@value = $id]
return
if ($itr) then
for $ptn in $itr
return
(rest:response
<http:response status="200">
{if ($ptn/p:Patient/p:meta/p:versionId/@value) then (
<http:header name="ETag" value="{$ptn/p:Patient/p:meta/p:versionId/@value}"/>
<http:header name="Content-Location" value="/Patient/{$id}/_history/{$ptn/p:Patient/p:meta/p:versionId/@value}"/>
) else (
<http:header name="Content-Location" value="/Patient/{$id}"/>
)}
<http:header name="Last-Modified" value="{$ptn/p:Patient/p:meta/p:lastUpdated/@value}"/>
</http:response>
</rest:response>, $ptn)
else
rest:response
<http:response status="404" message="Patient with id={$id} was not found."/>
</rest:response>
@@
throws exception at compilation phase:
Syntax error on line 15 at column 30 of near {... <http:header name=} XPST0003: expected ")", found name "name"
if I comment out the line 15, or wrap the lines 14, 15 in a block like http:b...</http:b> then it does work properly. Should I rewrite the query in some other way, or this is a bug? May be there is a better way to achieve the same in XQuery?
Thanks, Denis.
Replies (3)
Please register to reply
RE: Error in XQuery - Added by Michael Kay about 8 years ago
Write
then (, )
rather than
then( )
i.e. include a comma. Not a bug, it's just the way the syntax works. A sequence of element constructors with no intervening operator is not a valid expression.
In fact it's more complicated than that because @( < b)@ is a valid ValueComparison (compares with b), so the syntax error only occurs when you hit the second ">". It can be very difficult to produce good XQuery diagnostics for this kind of reason.
RE: Error in XQuery - Added by Denis Sukhoroslov about 8 years ago
Michael, thank you so much!
Yep, it does work now. I thought the '(' after 'then' is a kind of block statement, but it is a sequence opening.
Can I ask one more question? In the query above the values for $ptn/p:Patient/p:meta/p:versionId/@value and $ptn/p:Patient/p:meta/p:lastUpdated/@value are not empty. But the query considers the versionId value as empty and goes to the 'else' block, where it set Last-Modified value as empty as well. But the initial predicate /p:Patient[p:id/@value = $id] is resolved properly. I've attached the doc which conforms to the query.
Thanks, Denis
RE: Error in XQuery - Added by Denis Sukhoroslov about 8 years ago
Fixed. The p:Patient segment in the paths was redundant
Please register to reply