我可以为 XQuery 中的 XPath 步骤创建宏或快捷方式吗?
Can I create a macro or shortuct for a step of XPath in XQuery?
我们在 XQuery 中有宏吗?
如果是,能否请您举例说明它们的用法。
我有以下代码
let $x := //price/ancestor::*
我能不能用宏或者其他东西写成下面这样:
let $x := //price/outward
所以,outward
应该表示 ancestor::*
XQuery 不知道这样的宏,您当然可以使用任何预处理器来做这样的事情。
但我宁愿为此定义一个函数:
declare function local:outward($context as item()) {
$context/ancestor-or-self::*
};
函数也可以在axis steps中应用(记得传递当前上下文.
):
let $xml := document { <foo><bar><batz>quix</batz></bar></foo> }
return $xml/foo/bar/local:outward(.)
您甚至可以继续使用 "normal" XPath 表达式:
let $xml := document { <foo id="foo"><bar id="bar"><batz id="batz">quix</batz></bar></foo> }
return $xml/foo/bar/local:outward(.)/@id
除了 Jens 回答(使用函数)...如果目标不仅是语法糖,而且在某些时候让某人 "configure" 导航发生,您可以结合Jens 用功能项回答。在 XPath(和 XQuery)3.0 中,函数可以由函数项表示。一个函数项可以赋值给一个变量,并且可以用来调用它的函数"points to."
declare function local:outward($context as node()) {
$context/ancestor-or-self::*
};
declare function local:inward($context as node()) {
$context/descendant-or-self::*
};
declare function local:id($doc as node(), $axis as function(*)) {
(: note how we "call the variable $axis" :)
$doc/foo/bar/$axis(.)/@id
};
declare variable $input :=
document {
<foo id="foo"><bar id="bar"><batz id="batz"/></bar></foo> };
(: find the @id attributes in ancestors :)
local:id($input, local:outward#1)
,
(: find the @id attributes in descendants :)
local:id($input, local:inward#1)
我们在 XQuery 中有宏吗?
如果是,能否请您举例说明它们的用法。
我有以下代码
let $x := //price/ancestor::*
我能不能用宏或者其他东西写成下面这样:
let $x := //price/outward
所以,outward
应该表示 ancestor::*
XQuery 不知道这样的宏,您当然可以使用任何预处理器来做这样的事情。
但我宁愿为此定义一个函数:
declare function local:outward($context as item()) {
$context/ancestor-or-self::*
};
函数也可以在axis steps中应用(记得传递当前上下文.
):
let $xml := document { <foo><bar><batz>quix</batz></bar></foo> }
return $xml/foo/bar/local:outward(.)
您甚至可以继续使用 "normal" XPath 表达式:
let $xml := document { <foo id="foo"><bar id="bar"><batz id="batz">quix</batz></bar></foo> }
return $xml/foo/bar/local:outward(.)/@id
除了 Jens 回答(使用函数)...如果目标不仅是语法糖,而且在某些时候让某人 "configure" 导航发生,您可以结合Jens 用功能项回答。在 XPath(和 XQuery)3.0 中,函数可以由函数项表示。一个函数项可以赋值给一个变量,并且可以用来调用它的函数"points to."
declare function local:outward($context as node()) {
$context/ancestor-or-self::*
};
declare function local:inward($context as node()) {
$context/descendant-or-self::*
};
declare function local:id($doc as node(), $axis as function(*)) {
(: note how we "call the variable $axis" :)
$doc/foo/bar/$axis(.)/@id
};
declare variable $input :=
document {
<foo id="foo"><bar id="bar"><batz id="batz"/></bar></foo> };
(: find the @id attributes in ancestors :)
local:id($input, local:outward#1)
,
(: find the @id attributes in descendants :)
local:id($input, local:inward#1)