XQuery is complicated

It’s a rare week when I don’t discover some new corner of the language that I didn’t know about before. Did you know about per-variable typing?


let $a as xs:integer := 1
return $a + 2


=> 3

Sounds useless, doesn’t it? But just like function-parameter typing, it could prevent errors. Think about this:


let $a as element(ArticleTitle) :=
  doc($ID)/MedlineCitation/Article/ArticleTitle
return $a

Now we have a guarantee that there will be exactly one ArticleTitle. Anything else will cause an error:

XDMP-AS: let $a as element(ArticleTitle) :=
  doc($ID)/child::MedlineCitation/child::Article/child::ArticleTitle
  return $a -- Invalid coercion: () as element(ArticleTitle)

Nice, isn’t it?

Leave a Reply

You must be logged in to post a comment.