Hi,
Depends what you want to do with it (especially whether the map is to
be updated after its initialization). I recommend you encapsulate the
map with a function "get" that takes the name of the entry and return
the corresponding value (so the way it is implemented will not affect
the other parts of the application that use it).
Below are 3 different possibilities, depending on your constraints and
requirements. If you want the values from the map to be available in
other modules, you need to have that code in a library module and not
using the local: prefix, of course.
(: *** Version 1: Global map, init function. :)
declare variable $map := local:init();
declare function local:init() {
let $m := map:map()
let $_ := map:put($m, 'hasTaxonomyName',
'http://www.bsi.org/predicates/hasTaxonomyName')
return
$m
};
declare function local:get($name as xs:string) as xs:string? {
map:get($map, $name)
};
local:get('hasTaxonomyName')
(: *** Version 2: Global map, direct initialization. :)
declare variable $map :=
let $m := map:map()
let $_ := map:put($m, 'hasTaxonomyName',
'http://www.bsi.org/predicates/hasTaxonomyName')
return
$m;
declare function local:get($name as xs:string) as xs:string? {
map:get($map, $name)
};
local:get('hasTaxonomyName')
(: *** Version 3: Global map, using an XML structure. :)
declare variable $map :=
<suffixes>
<entry
suffix="hasTaxonomyName">http://www.bsi.org/predicates/hasTaxonomyName</entry>
</suffixes>;
declare function local:get($name as xs:string) as xs:string? {
$map/entry[@suffix eq $name]
};
local:get('hasTaxonomyName')
Hope that helps, regards,
--
Florent Georges
http://fgeorges.org/
http://h2oconsulting.be/
On 27 July 2015 at 12:24, Tyagi, Devesh wrote:
> Thanks Florent and Geert,
>
> The real motive behind this question is to implement a globally available map
> for all the functions of the xquery module. Any suggestions about how to
> implement it? May be I can put it in a different Xquery module and import it?
>
> Regards,
> Devesh
>
> ________________________________________
> From: [email protected]
> <[email protected]> on behalf of Florent Georges
> <[email protected]>
> Sent: Monday, July 27, 2015 1:55 PM
> To: MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] XQuery evaluation
>
> Hi,
>
> Yet a cleaner solution would be to make the init function to return
> the map to assign to the global variable, so there is no hidden
> dependency:
>
> declare variable $global-map := my:init();
> declare function my:init() {
> let $map := map:map()
> ...
> return
> $map
> };
>
> If the init code is that simple, the let expression can then even be
> used straight in the variable value expression, without using a
> function.
>
> Avoiding side-effects when none is required will save you some
> (hard-to-track-down) troubles.
>
> Regards,
>
> --
> Florent Georges
> http://fgeorges.org/
> http://h2oconsulting.be/
>
>
> On 27 July 2015 at 10:16, Geert Josten wrote:
>> Hi Devesh,
>>
>> XQuery specification allows lazy evaluation, and MarkLogic makes heavy use
>> of that. Just don’t put the call to your init function in a declare
>> variable, but simply call it before the local:test. Something like:
>>
>> let $init := local:initialize-variables()
>> return
>> local:test()
>>
>> Cheers,
>> Geert
>>
>> From: <[email protected]> on behalf of "Tyagi, Devesh"
>> <[email protected]>
>> Reply-To: MarkLogic Developer Discussion <[email protected]>
>> Date: Monday, July 27, 2015 at 9:55 AM
>> To: "[email protected]" <[email protected]>
>> Subject: [MarkLogic Dev General] XQuery evaluation
>>
>> Hi,
>>
>>
>> I have the following piece of code in XQuery:
>>
>>
>> declare variable $triple-predicate-prefix :=
>> "http://www.bsi.org/predicates/";
>>
>> declare variable $triple-predicate-suffix-hasTaxonomyName :=
>> "hasTaxonomyName";
>>
>>
>> declare variable $triple-predicates-map := map:map();
>>
>> declare variable $initialize-variables-caller :=
>> local:initialize-variables();
>>
>>
>> declare function local:initialize-variables(){
>>
>> let $_ :=
>> map:put($triple-predicates-map,$triple-predicate-suffix-hasTaxonomyName,fn:concat($triple-predicate-prefix,$triple-predicate-suffix-hasTaxonomyName))
>>
>> return ()
>>
>> };
>>
>>
>> declare local:test(){
>>
>> map:get($triple-predicates-map,$triple-predicate-suffix-hasTaxonomyName)
>>
>> };
>>
>>
>> local:test()
>>
>>
>> This returns me and empty sequence. The map doesn't get initialized. It
>> would be helpful if someone could point me towards the reason and solution
>> behind it.
>>
>>
>> Thanks and regards,
>>
>> Devesh
>>
>> "This e-mail and any attachments transmitted with it are for the sole use of
>> the intended recipient(s) and may contain confidential , proprietary or
>> privileged information. If you are not the intended recipient, please
>> contact the sender by reply e-mail and destroy all copies of the original
>> message. Any unauthorized review, use, disclosure, dissemination,
>> forwarding, printing or copying of this e-mail or any action taken in
>> reliance on this e-mail is strictly prohibited and may be unlawful."
>>
>> _______________________________________________
>> General mailing list
>> [email protected]
>> Manage your subscription at:
>> http://developer.marklogic.com/mailman/listinfo/general
>>
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
> "This e-mail and any attachments transmitted with it are for the sole use of
> the intended recipient(s) and may contain confidential , proprietary or
> privileged information. If you are not the intended recipient, please contact
> the sender by reply e-mail and destroy all copies of the original message.
> Any unauthorized review, use, disclosure, dissemination, forwarding, printing
> or copying of this e-mail or any action taken in reliance on this e-mail is
> strictly prohibited and may be unlawful."
> _______________________________________________
> General mailing list
> [email protected]
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
Manage your subscription at:
http://developer.marklogic.com/mailman/listinfo/general