> bxcode:inline-variables($xqcode-uri) as xs:string > @returns xquery code that is the source code from $xqcode-uri but with > inlining "constant" variables and operations applied > > This could be used as a preprocessor. > Is this something that could be created from the bits and pieces in the > current BaseX Java code?
This reminds me of the discussion with Omar [1]: There is currently no way to generate a correct string representation for compiled code. In the GUI Info View, we output a string representation of the optimized query. In simple cases, this string is a valid and equivalent to the original query string, but it’s fairly easy to generate invalid results. If it’s an option for you to generate code in XQuery, something like the following could be done: _ bxcode.xqm _______ module namespace bxcode = 'bxcode'; declare function bxcode:bind-external-variables( $uri as xs:string, $vars as map(xs:string, xs:string) ) as xs:string { let $xqcode := unparsed-text($uri) return fold-left( map:keys($vars), $xqcode, function($string, $name) { let $value := replace($vars($name), ']``', '`{"]``"}`', 'q') return $string => replace('declare variable $' || $name || ' external;', '', 'q') => replace('$' || $name, '``[' || $value || ']``', 'q') } ) }; _ example.xq _______ declare variable $db external; count(collection($db)) _ run-bxcode.xq _______ import module namespace bxcode = 'bxcode' at 'bxcode.xqm'; bxcode:bind-external-variables('query.xq', map { 'db': 'BEP' }) _ output _______ count(collection(``[BEP]``)) Cheers, Christian [1] https://www.mail-archive.com/basex-talk@mailman.uni-konstanz.de/msg09863.html