Drop the "fn:" in MarkLogic functions?
Asked Answered
S

1

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!

Sarawak answered 22/10, 2013 at 23:7 Comment(0)
E
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.)

Encephalogram answered 22/10, 2013 at 23:35 Comment(5)
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.Lubricous
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.Alec
OK, using your code there as a model, I think I got it: module namespace my-namespace = '<my-namespace>'; declare default function namespace "w3.org/2005/xpath-functions";Alec
Yeah, MarkMail was written against xquery version "0.9-ml" and you're on "1.0-ml" probably. I'll tweak my answer.Encephalogram
Now... if there were also a way to pull in functx functions into the default namespace as well... heh :) ... thanks for your help!Alec

© 2022 - 2024 — McMap. All rights reserved.