XQuery:在(导入的)模块之间共享全局变量 [英] XQuery: sharing global variables across (imported) modules

查看:71
本文介绍了XQuery:在(导入的)模块之间共享全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令我惊讶的是,我正在处理一个非常基本的XQuery问题,即在主要XQuery模块和导入的库模块之间共享全局变量的正确方法是什么.简而言之,我想在某个地方定义一个全局变量,该变量可以在任何地方重用(即在所有(导入的)XQuery模块中),并且正在努力寻找声明此类变量的最佳位置.

To my surprise, I'm wrestling with a quite basic XQuery question, namely what is the correct way to share global variables between a main XQuery module and imported library modules. Put simply, I would like to define a global variable somewhere that can be reused anywhere (i.e. in all (imported) XQuery modules), and am struggling to find the best place to declare such a variable.

假设我有以下主要XQuery(test.xq):

Suppose I have following main XQuery (test.xq):

import module namespace global="global" at "global.xq";
import module namespace test2="test2" at "test2.xq";

declare variable $test := 'test!';

test2:echo()

此模块导入以下库模块:

This module imports following library modules:

  • global.xq:

  • global.xq:

module namespace global="global";

declare variable $global:test := 'global!';

  • test2.xq:模块名称空间test2 ="test2";

  • test2.xq: module namespace test2="test2";

    import module namespace global="global" at "global.xq";
    
    declare function test2:echo() {
      $global:test
    };
    

  • 这可行,但是给我一些问题:

    This works, but leaves me with some questions:

    • 是这样吗?

    • Is this the way to do it:

    • 在单独的库模块(例如global.xq)中定义全局变量(例如$ global:test)
    • 在需要访问其变量的位置导入该模块

    ?

    有人可以阐明这一点吗?我想我自己为这个概念而苦恼的主要原因是因为我习惯了 eXist 的行为,这可能比应该的宽松.在eXist中,test2.xq模块可以仅引用$ global:test变量,而无需导入global.xq模块:

    Can anyone shed light on this? I guess the main reason why I find myself struggling with this concept is because I'm used to eXist's behaviour, which is probably laxer than it should. In eXist, the test2.xq module can just refer to the $global:test variable without importing the global.xq module:

    module namespace test2="test2";
    declare namespace global="global";
    
    declare function test2:echo() {
      $global:test
    };
    

    由于这在eXist中有效,但在Saxon中不适用,所以我开始想知道在(导入的)XQuery模块中定义和使用全局变量的正确方法是什么.

    Since this works in eXist, but not in Saxon, I started wondering what is the correct way to define and use global variables in (imported) XQuery modules.

    亲切的问候,

    罗恩

    推荐答案

    eXists显然采用了XSLT所熟悉的方法.在这里,您还可以通过import/includes引用当前模块外部声明的变量和参数.的祖先"模块(在包含/导入链中更高).

    Apparently eXists takes the approach that might be familiar to you from XSLT.In there you can also refer to variables and parameters declared outside the current module through imports/includes of 'ancestor' modules (higher in the include/import chain).

    据我所知,不符合XQuery标准!

    能够使用它显然很实用,但是有一些很好的理由不这样做.理想情况下,模块是独立的,可重用的组件,而当它们依赖于此类外部参数/变量时,情况就不再如此.最好将上下文信息作为参数传递给此类函数.并不总是看起来优雅,但最终还是更好的设计.

    It is obviously very practical to be able to use this, but there are a few good reasons why not to do so. Ideally, modules are self-contained, reuseable components, and that is no longer the case when they rely on such external params/vars. It would be better to pass in contextual information as a parameter to such functions. Doesn't always look elegant, but is better design in the end.

    某些实现使用重新定义变量值来提供替代选项.MarkLogic具有xdmp:set命令,并且我相信saxon也具有变量赋值(或者是在XSLT中吗?).您可以使用它来初始化"模块.请注意,尽管这些模块不是对象,所以请避免使用这种方法来保存有状态信息.这也意味着您将依赖于实现特定的功能.除非您能够为此使用更新工具.不确定这样做是否可以.

    Some implementations provide an alternative option using redefinition of variable values. MarkLogic has the xdmp:set command, and I believe saxon has variable assignment too (or was that in XSLT?). You could use that to 'initialize' modules. Mind though that modules aren't objects, so try to avoid using such an approach to hold statefull information. It would also mean you'd rely on implementation specific functions. Unless you'd be able to use the update facility for that. Not sure that is intended to work that way though..

    这篇关于XQuery:在(导入的)模块之间共享全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆