Difference between importing and declaring module namespace in XQuery? - xquery

What is the difference between the following:
import module namespace fs = "http://expath.org/ns/file";
declare namespace an = "http://zorba.io/annotations";
How does "import module namespace" compare to "declare namespace"?
And more over, with namespace decalaration waht is the difference between
declare namespace an = "http://zorba.io/annotations";
and
module namespace an = "http://zorba.io/annotations";

The module namespace will allow you to use xquery functions from various modules. This is like using libraries in other languages. For example the functx library:
import module namespace functx="http://www.functx.com"
functx:substring-before-match('abc-def-ghi', '[dg]')
If you would want to create your own module, 'mymodule.xq' you would begin the file with a module declaration:
module namespace mymodule = "http://example.org/mymodule";
declare function mymodule:myfunc()....
declaring namespaces allows you to create and query xml elements using different namespaces.
For example:
declare namespace x="http://some.random.namespace";
//x:someelement[. = 'hello world']
will query xml elements that have the 'x' namespace.
Now in your case regarding the zorba annotations. Declaring a namespace is really just saying to the xquery processor: this prefix (an) is bound to this namespace (http://zorba.io/annotations). I'm not really sure how to explain it further, it just the way it has been defined in the xquery spec. It's just to tell the xquery processor that if you type:
declare %an:nondeterministic function random:random() as xs:integer external;
that 'an' is bound to 'http://zorba.io/annotations' which is something that zorba will understand.
You might just as well change 'an' to 'foo':
declare namespace foo = "http://zorba.io/annotations";
declare %foo:nondeterministic function random:random() as xs:integer external;
and zorba would still be able to understand it.

Related

Need a way to backport serialize() to xquery1.0

I'd like to put an xml node as CDATA in the output document and I have to use xquery1.0
How can I backport serialize()?
I have Saxon-HE-9.5.1-8.jar as XQuery processor provided by wso2-mi
I don't know details about Saxon 9.5 but it is not an old, pure XQuery 1.0 processor but somewhere between the two versions; so perhaps trying
xquery version "3.0";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method 'xml';
declare option output:cdata-section-elements 'foo';
works.

what is an XQuery to import module namespace in MarkLogic

What is an XQuery to import module namespace in MarkLogic? How do you bind the namespace to the prefix admin?
import module namespace admin="http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy";
I tried it this way but getting error.
ServerEvaluationCall forestDataDirCall =
client
.newServerEval()
.xquery("xquery version \"1.0-ml\";\r\n" +
"xdmp:with-namespaces(admin,http://marklogic.com/xdmp/admin)
The import statement that you referenced does two things:
declares the namespace "http://marklogic.com/xdmp/admin" bound to the admin namespace-prefix
imports the "/MarkLogic/admin.xqy" library module that is bound to that admin namespace, so that it's functions can be used and referenced by the admin prefix.
You can also simply declare a namespace and bind it to a namespace-prefix
declare namespace admin="http://marklogic.com/xdmp/admin";
However, then you would not have those library functions available to be used.
It appears that this question is related to one of your other questions. In order to eval code using admin functions, you need to include the import statement in the XQuery that you want to evaluate:
ServerEvaluationCall forestDataDirCall = client.newServerEval()
.xquery('import module namespace admin="http://marklogic.com/xdmp/admin" at "/MarkLogic/admin.xqy"; admin:forest-get-data-directory(admin:get-configuration(), admin:forest-get-id(admin:get-configuration(), "' + forestName +'"))');

How do I get the HTTP POST body in MarkLogic XQuery?

I'm trying to use the exist-db request:get-data() method to get the post data of a request. However, I'm getting the error:
XDMP-UNDFUN: (err:XPST0017) Undefined function request:get-data()
I did declare the namespace in my header. I don't understand why I still can't use request:get-data() or any of the other request: functions
declare namespace request="http://exist-db.org/xquery/request";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
let $post-data := request:get-data()
return $post-data
I think you're looking for xdmp:get-request-body.
Sam pointed you to the function you need, but I wanted to respond to another part of your question:
I did declare the namespace in my header. I don't understand why I still can't use request:get-data() or any of the other request: functions
Each XQuery processing engine implements standard functions, but there is other functionality needed that is not defined by the standard. For MarkLogic, you'll use standard functions with the fn: prefix.
Each XQuery engine then defines additional functions that will be needed. For Exist DB, some of those are in the "http://exist-db.org/xquery/request" namespace, while MarkLogic uses "http://marklogic.com/xdmp" for a lot of its extension functions.
When you're looking for the MarkLogic equivalent of an Exist DB-specific function, search on http://docs.marklogic.com -- start with the function name, and if that doesn't work, search for the terms that describe what you're trying to do.

How to import common module namespaces in xquery

I have few module namespace xquery files which were used in multiple files. I want to have the namespaces in one common xquery file and import that file whereever I want to use.
Say for example,
I have process-lib.xqy, util-lib.xqy and query-lib.xqy.
I used to import these in multiple files like following,
import module namespace util = "util" at "util-lib.xqy";
import module namespace process = "process" at "process-lib.xqy";
import module namespace query = "query" at "query-lib.xqy";
Now I tried to use them in one common file named as common-import.xqy and import this file in multiple files.
when I tried this approach,
import module namespace common-import= "common-import" at "common-import.xqy";
It throws exception as prefix util has no namespace binding.
How to achieve this?
This is not possible, at least not in the way you want to do it and rightfully so. The XQuery spec doesn't allow this:
Module imports are not transitive—that is, importing a module provides access only to function and variable declarations contained directly in the imported module. For example, if module A imports module B, and module B imports module C, module A does not have access to the functions and variables declared in module C.
This is a deliberate design decision. If you want to have access in this way you could write a wrapper function for each function you want to access, e.g. in your common-import.xqy file you could have:
declare function common-import:test() {
util:test()
};
But of course this can require a tremendous amount of wrapper functions. I would recommend you stick simply to inserting all required libraries. I see no benefit in doing otherwise.

declare global namespace variable from TypeScript

How to explicitly declare a variable in the global namespace from typescript?
I need the compiler to generate the following javascript code:
MyExtension = someFunction()
unfortunately, I can only have it generate
var MyExtension = someFunction()
This comes to an issue with the latest version (still in rc) of meteor packages.
Meteor introduced a way to scope namespaces in packages - the issue is, the variable needs to be defined in the global namespace (which meteor reroutes to its own Package object).
There is a video about it at https://www.eventedmind.com/posts/meteor-linker-package-namespacing.
Is there some kind of global keyword available or in the plans?
Use the declare keyword. These are known as ambient declarations.
declare var MyExtentention:any;

Resources