7

Is there a way with MarkLogic to not have to prefix every single fn: function with that prefix? I've seen lots of codes on the Internet that show me that I don't need it.

Things can get rather verbose, you know? fn:not(fn:contains(...)), instead of not(contains(...))

Thoughts?

Thanks!

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52

1 Answers1

9

Like you, I prefer not to type fn: in front of all my fn:functions.

In normal XQuery main modules you don't need the fn: prefix because that's the default function namespace and used for all unprefixed functions. You do however need fn: in library modules because they change their default function namespace to that of the library module namespace. This means the library functions can call each other without any prefix.

But you can change it back! Here's the header code to do the switch back.

xquery version "1.0-ml";
module namespace util = "http://markmail.org/util";
declare default function namespace "http://www.w3.org/2005/xpath-functions";

Or if you're on the older 0.9-ml:

xquery version "0.9-ml"
module "http://markmail.org/util"
declare namespace util = "http://markmail.org/util"
default function namespace = "http://www.w3.org/2003/05/xpath-functions"

It puts the module in a given namespace, assigns util to that namespace, then assigns the default back to the normal fn: one.

After this switch, function calls and definitions without a prefix will default to the fn: prefix; that means all functions in the util library should explicitly use a util: prefix. (Personally, I think that's cleaner anyway.)

hunterhacker
  • 6,378
  • 1
  • 14
  • 11
  • 1
    There's a feature coming in V7 that will allow you to specify a set of default namespaces for a given application server and I believe the default set now includes fn. – Eric Bloch Oct 22 '13 at 23:38
  • Sweet. Didn't know that about that in the main modules. Works as advertised there. However, the switch doesn't work. I'm on ML5, and I get this error when trying the code you provided: "Unexpected token syntax error, unexpected StringLiteral_, expecting Namespace_". I assume this is related to the ML version. – Mr Mikkél Oct 23 '13 at 16:36
  • OK, using your code there as a model, I think I got it: module namespace my-namespace = ''; declare default function namespace "http://www.w3.org/2005/xpath-functions"; – Mr Mikkél Oct 23 '13 at 16:57
  • Yeah, MarkMail was written against xquery version "0.9-ml" and you're on "1.0-ml" probably. I'll tweak my answer. – hunterhacker Oct 23 '13 at 17:23
  • Now... if there were also a way to pull in functx functions into the default namespace as well... heh :) ... thanks for your help! – Mr Mikkél Oct 23 '13 at 17:34