Two reasons your MarkLogic code is failing silently, part 2

Silent failures are a programmer’s worst nightmare, and in a world where first class debuggers are few and far between, those silent failures are sure to drive us crazy. In the first half of this article, we discussed Two reasons your MarkLogic code is failing silently, part 1 silent failures in MarkLogic due to XML namespace issues. XML namespace issues are going to crop up in any implementation of XQuery, though, so there are many resources to reference in addition to my article.

In the second (and last) part of the article, we’ll discuss a feature specific to MarkLogic’s XQuery implementation. This feature is called function mapping.

[Read More]

Two reasons your MarkLogic code is failing silently, part 1

If you’re new to MarkLogic application programming, it’s easy to get frustrated when your code fails without any obvious reason why.  There’s no run-time error, no exception to catch, and when you surround the code block in question with log writing expressions, the “before” log expression and the “after” log expression both work perfectly.

Even now, excellent application developers are still making these same mistakes because they’re so easy to do if you aren’t paying perfect attention.

Coworker: Hey, can you look at this code and see if you know what’s wrong with it?

Me: (before he pastes the code into the IM window)

Me: namespaces or function mapping

Coworker: …

Coworker: Damnit.

[Read More]

Convert an integer to a sequence of digits in Xquery

I am new to the functional programming paradigm and Xquery. As part of a larger exercise, I wanted to sum the digits of an integer.  The first part of that problem is to solve the smaller problem: convert an integer to a sequence of integers that I can then pass to fn:sum as a parameter.

(: I've declared the function in the nwnu namespace for testing purposes :)
declare function nwnu:number-to-seq($num as xs:integer)
as xs:integer* {
  if($num gt 9) then
    (
      nwnu:number-to-seq(xs:integer(math:floor($num div 10))),
      xs:integer(math:floor($num mod 10))
    )
  else
    $num
};
[Read More]