what is an XQuery to import module namespace in MarkLogic - xquery

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 +'"))');

Related

How can you use functions from outside a module inside a module

Basically I want a module that can use functions that are outside its scope.
I need this because my work will only provide a framework where the user will be able to put its own code in. Something like this
Simulation.jl
abstract AbstractType
function startSimulation(unknown::AbstractType)
doStuff(unknown)
end
MyModule.jl
module MyModule
include("Simulation.jl")
export AbstractType, startSimulation
end
SomeImplementation.jl
type ConcreteType <: AbstractType
variable::Int64
end
doStuff(me::ConcreteType)
me.variable
end
and finally Main.jl
# push!(LOAD_PATH, pwd()) # for local usage
using MyModule
include("SomeImplementation.jl")
startSimulation(ConcreteType(5))
Where Simulation.jl and MyModule.jl are written by me and SomeImplementation.jl and Main.jl are written by the user.
Now the above example does not work, because modules have their own namespace and even tho SomeImplementation.jl is imported in main at line 3, the interpreter will not see it in line 4 of Simulation.jl.
I can not import anything in MyModule.jl, because I can not know how the user will name his stuff or what extra libs he might even need.
Is there are way to do this with modules? Otherwise I will just not use modules.
The answer here is to create stubs for all the functions you want to call within MyModule as a required interface for the custom subtypes of AbstractType. That is, within MyModule, you'd have:
abstract AbstractType
doStuff(::AbstractType) = error("custom AbstractType objects must define a `doStuff` method)
function startSimulation(unknown::AbstractType)
doStuff(unknown)
end
Then the concrete implementations just need to specifically add their doStuff method to the function in MyModule by importing it or qualifying it:
MyModule.doStuff(me::ConcreteType)
me.variable
end

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.

Difference between importing and declaring module namespace in 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.

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;

Add external libraries to Symfony2 project

I am trying to add an external library (PHP Simple DOM Parser, http://simplehtmldom.sourceforge.net/index.htm) to a Symfony2 project. I took a tutorial that explains how to include third party libraries to Symfony2 http://www.kiwwito.com/article/add-third-party-libraries-to-symfony-2.
I set up a class file like:
# vendor/phpsimpledom/lib/Phpsimpledom/simple_html_dom.php
require_once __DIR__.'/src/simple_html_dom.php';
class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {
}
and registered my class in my Autoloader (autoload.php):
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/',
...
),));
I am trying to call:
$phpsimpledom = new \Phpsimpledom();
but this throughs me an error (Fatal error: Class 'simple_html_dom_node' not found).
However: The main file of the library (simple_html_dom.php) consists of functions that do not belong to a class.
When I try to use the file directly, it also doesn't work:
$loader->registerNamespaces(array(
...
'Phpsimpledom' => __DIR__.'/../vendor/phpsimpledom/lib/Phpsimpledom/src/simple_html_dom.php',
...
),));
Any hints?
THANKS!
You're trying to register a namespace but your class has no namespace. Try adding a namespace to it or use RegisterPrefixes().
BTW: did you know that one of the Symfony components is basically doing the same thing as php simpledom? It's called DomCrawler and it has a support for both xpath and CSS selectors.
I'm new to Symfony2 but as i can see, you are not respecting the PSR for autoloader.
I'm presumable thinking you should do:
# /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php
require_once __DIR__.'/src/simple_html_dom.php';
class Phpsimpledom_Phpsimpledom extends simple_html_dom_node {
}
Note that the correct filename would be /vendor/phpsimpledom/lib/Phpsimpledom/Phpsimpledom.php as the call must include the namespace to work.
Hope it works now.

Resources