Skip to content

Instantly share code, notes, and snippets.

@dmccreary
Created December 31, 2013 20:36
Show Gist options
  • Save dmccreary/8201987 to your computer and use it in GitHub Desktop.
Save dmccreary/8201987 to your computer and use it in GitHub Desktop.
Hot to detect what version of XQuery you are running using the newer XQuery 3.0 function-lookup() function. Note: this approach will not work on XQuery 1.0.
xquery version "3.0";
(: Sample XQuery program to detect what version of XQuery you are running.
Note this ONLY works in XQuery 3.0. It will fail in any XQuery "1.0" implementation such as Saxon HE.
Unlike many things in XQuery, this function is not elegant. Perhaps XQuery "4.0" will include a function such system-property('version')
Note that fn:function-lookup() returns a function if it exists and null if it does not.
See http://www.w3.org/TR/xpath-functions-30/#func-function-lookup
Thanks to http://stackoverflow.com/users/1451599/dirkk for the solution
http://stackoverflow.com/questions/20849295/how-can-i-write-an-xquery-that-can-test-what-version-of-xquery-is-being-used
:)
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"
};
<results>
{local:get-processor()}
</results>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment