您可以使用 XQuery 3.0 函数function-lookup
来检查是否存在特定于实现的函数来检查处理器。所有支持 XQuery 3.0 的处理器都应该能够处理这个问题。
declare function local:exist() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('system:get-version'), 0))))
then true()
else false()
} catch * {
false()
}
};
(: works only on Saxon PE and EE, fails in HE since HE does not support XQuery 3.0. It would be nice if saxon had a version function. :)
declare function local:saxon() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('saxon:parse'), 1))))
then true()
else false()
} catch * {
false()
}
};
declare function local:marklogic() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('xdmp:xquery-version'), 0))))
then true()
else false()
} catch * {
false()
}
};
declare function local:basex() as xs:boolean {
try {
if (not(empty(function-lookup(xs:QName('prof:time'), 1))))
then true()
else false()
} catch * {
false()
}
};
declare function local:get-processor() as xs:string {
if (local:exist()) then "eXist"
else if (local:saxon()) then "Saxon"
else if (local:marklogic()) then "MarkLogic"
else if (local:basex()) then "BaseX"
else "Unknown"
};
local:get-processor()
不幸的是,我无法使用高阶函数使它更优雅,因为特定于实现的函数是在实际调用函数之前执行的。但我相信这也可以写得更优雅。