XQuery 函数命名空间如何工作?

发布于 2024-11-17 07:45:44 字数 821 浏览 2 评论 0原文

编辑

我想将相关功能分组在一起以表明它们是相关的。

如果我有 local:f1()local:f2() 那么我可以将它们的名称更改为 local:menu-f1()和 local:menu-f2() 但 XQuery 语言中是否有一种机制可以对相关函数进行分组?

OP

我很高兴发现 XQuery 函数可以在 local: 以外的命名空间中声明。我在哪里可以找到有关其工作原理的信息?

总是以这种方式声明函数;

declare function local:foo() {
   3+4
};

..并以这种方式使用它们;

local:foo()

..我发现它们可以这样声明;

declare namespace baz = "fred:bloggs";
declare function baz:foo() {
   3+4
};

..并像这样使用;

baz:foo()

但我只能分别找到有关声明命名空间声明函数的类似参考的信息,而不是有关 XQuery 函数命名空间一般如何工作的类似教程的信息。

有 XQuery 函数命名空间的新手指南吗?

我正在使用 Saxon 处理器 - XQuery 1.0。

EDIT

I want to group together related functions to show that they are related.

If I have local:f1() and local:f2() then I could just change their names to local:menu-f1() and local:menu-f2() but is there a mechanism in the XQuery language to group related functions?

OP

I am very excited to discover that XQuery functions can be declared in a namespace other than local:. Where can I find info about how this works?

Having always declared functions in this way;

declare function local:foo() {
   3+4
};

.. and used them in this way;

local:foo()

.. I discover that they can be declared like this;

declare namespace baz = "fred:bloggs";
declare function baz:foo() {
   3+4
};

.. and used like this;

baz:foo()

But I can only find reference-like information about declare namespace and declare function separately, not tutorial-like information about how XQuery function namespaces work in general.

Is there a newbie guide to XQuery function namespaces?

I'm using a Saxon processor - XQuery 1.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-11-24 07:45:44

您可能使用的是普通的 XQuery 命名空间 - 您可能正在寻找的是模块。您可以将一堆函数放入其自己的模块命名空间中,如下所示:

module namespace foo = "http://www.myurl.com/foo";

declare function foo:bar($args as item()*) as item()* {
  () (: do something cool :)
};

然后您可以在主查询中导入模块并调用该函数:

import module namespace foo = "http://www.myurl.com/foo";

foo:bar(<my-element/>)

问题是,它不是标准化的,处理器如何找到查询。而且我不知道 Saxon 如何实现模块解析机制(您应该查看文档和/或写入 Saxon 邮件列表)。

但大多数 XQuery 处理器都会查看由“at”子句给出的相对于查询位置的路径。因此,要拥有适用于大多数实现的东西:例如,您可以将模块存储在名为 foo.xq 的文件中,并将其放入与主查询相同的目录中,然后对于模块导入,您将编写:

import module namespace foo = "http://www.myurl.com/foo" at "foo.xq";

这给出了提示到 XQuery 引擎,它应该在其中查找模块。

您可以在 http://www.xquery.me/< 找到一些(目前不是很多)有关此内容的文档/a> - 希望这有帮助。

编辑

好的,我明白了,您只想对功能进行分组。为此,您已经了解了需要了解的所有内容。但我仍然想强调,将查询拆分为模块可能是适合您的用例的更好解决方案(它只是在某种程度上更好,因为您有更多的模块化性,并且在即将推出的 XQuery 3.0 建议中,您甚至可以将诸如私有函数和变量之类的东西)。但如果你的查询没有变大,你的解决方案当然也可以。
您可以像考虑 C++ 中的命名空间一样来考虑 XML 命名空间。在 XQuery 中,函数、元素、集合、变量、属性等可以位于自己的命名空间中(同样 - 就像在 C++ 中一样)。有一些隐式定义的命名空间,例如 xs(XML 模式命名空间,您可以在其中找到布尔值、整数等数据类型)、local(可以在其中放入函数的命名空间,这样您就不必在主查询)、fn(其中定义了“XQuery 1.0 和 XPath 2.0 函数和运算符”建议中的所有函数)。但这个函数的前缀只是一个别名——你可以使用任何你想要的。
因此,假设您的查询的序言中有以下代码:

declare namespace blubb = "http://www.w3.org/2001/XMLSchema";

blubb:integer 与 xs:integer 的类型完全相同 - 对于函数来说也是如此:

declare namespace l = "http://www.w3.org/2005/xquery-local-functions";

声明您可以使用“访问本地命名空间中的每个函数” l" 前缀(如果 local:bar() 存在,则 l:bar() )。

如果不键入前缀,XQuery 假定该函数位于“fn”名称空间中。这就是为什么 bot

fn:concat("Hello ", "World!")

concat("Hello ", "World!")

bot是等价的。您可以更改此行为。您可以将此行包含到序言中:

declare default function namespace "http://www.w3.org/2005/xquery-local-functions";

这将告诉 XQuery 处理器您不想为本地函数添加前缀(因此 bar() 相当于 local:bar())。

我不确定我是否回答了你的问题,或者至少能够澄清一些问题。我不知道这方面的教程(因为一开始它有点令人困惑,但最终您意识到没有太多可说的,因为这些机制比它们一开始看起来要简单得多)。我总是查找内容的文档是 http://www.w3.org/TR/ 上的推荐xquery/

如果这对您没有帮助,请尝试获得资格,我可以再试一次并提供解释。

What you are probably using are normal XQuery namespaces - what you probably are looking for are modules. You can put a bunch of functions in its own module namespace like this:

module namespace foo = "http://www.myurl.com/foo";

declare function foo:bar($args as item()*) as item()* {
  () (: do something cool :)
};

Afterwards you can import the module in you main query and call the function:

import module namespace foo = "http://www.myurl.com/foo";

foo:bar(<my-element/>)

The problem is, that it is not standardized, how the processor has to find the query. And I don't know how Saxon implements the module resolving mechanism (you should look into the documentation and/or write to the Saxon mailing list).

But most XQuery processors look at the path given by an "at" clause relative from the location of the query. So to have something that should work on most implementations: For example you could store the module in a file named foo.xq and place it into the same directory than your main query and then for the module import you would write:

import module namespace foo = "http://www.myurl.com/foo" at "foo.xq";

which gives a hint to the XQuery engine where it should look for the module.

You can find some (not a lot at the moment) documentation about this stuff at http://www.xquery.me/ - hope this helps.

EDIT

Ok I see, you only want to group your functions. To do that you already figured out everything you need to know. But I still want to emphasize that splitting your query up into modules would probably be the better solution for your use-case (it's just somehow nicer, since your have more modularity and in the upcoming XQuery 3.0 recommendation you will even have the possibility to put stuff like private functions and variables in there). But if your query does not get big, your solution is of course also ok.
You can think about XML namespaces the same way you would think about namespaces in C++. In XQuery, functions, elements, collections, variables, attributes etc can be in an own namespace (again - like in C++). There are some implicitely defined namespaces like xs (the XML Schema namespace where you can find the data types like boolean, integer etc), local (a namespace where you can put in functions so that you are not forced to define your own namespace in a main query), fn (where all functions from the "XQuery 1.0 and XPath 2.0 functions and operators" recommendation are defined). But the prefix of this function is only an alias - you can use whatever you want.
So let's say you have the following code in the prolog of your query:

declare namespace blubb = "http://www.w3.org/2001/XMLSchema";

blubb:integer would be exactly the same type than xs:integer - the same holds for functions:

declare namespace l = "http://www.w3.org/2005/xquery-local-functions";

With declaring that you can access every function in the local namespace with the "l" prefix (so l:bar() if local:bar() exists).

If you do not type a prefix, XQuery assumes that this function is in the "fn" namespace. This is why bot

fn:concat("Hello ", "World!")

and

concat("Hello ", "World!")

are equivalent. You can change this behavior. You could include this line into the prolog:

declare default function namespace "http://www.w3.org/2005/xquery-local-functions";

which would tell the XQuery processor that you do not want to prefix local functions (so bar() would be equivalent to local:bar()).

I am not sure if I answered your questions or at least was able to bring in some clarity. I do not know about a tutorial for that (since in the beginning it is somehow confusing but in the end you realize that there is not a lot to say about since the mechanisms are much simpler than they look in the first place). The document where I always look up stuff is the recommendation at http://www.w3.org/TR/xquery/

If this does not help you please try to qualify and I can try again with an explanation..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文