1

I want to call a XQuery function with JavaScript to retrieve data from a XML file. I'm able to call this simple function which doesn't read anything from any file:

<script type="text/javascript" 
        src="mxqueryjs/mxqueryjs.nocache.js"
        ></script>
<script type="application/xquery">
   module namespace m = "http://www.xqib.org/module";

   declare function m:GetNearestLocations($node as node()+) {
     let $message := "Hello XQuery!"
     return $message

   };
</script>

With this JavaScript:

var output = xqib.call(
    'http://www.xqib.org/module',
    'GetNearestLocations',
    center.lat());

The return output is as expected "Hello XQuery!".

Now I want to import a math module so that I can use some of its functions while reading data from a XML file.

Here's what I have but the math module doesn't import and causes XQST0059 error saying that there's no information location to load module with namespace "http://www.w3.org/2005/xpath-functions/math":

<script type="text/javascript" 
        src="mxqueryjs/mxqueryjs.nocache.js"
        ></script>
<script type="application/xquery">
   module namespace m = "http://www.xqib.org/module";
   import module namespace math
     = "http://www.w3.org/2005/xpath-functions/math";

   declare function m:GetNearestLocations($node as node()+) {
     let $message := "Hello XQuery!"
     return $message

   };
</script>

What's is weird about this is that when I use Stylus Studio X15 Entreprise Suite to test the same function the import is working.

Important: I'm using the same JavaScript call when I import or not the Math module, so maybe my problem comes from there but, I don't know how I could fix this.

If you could also guide me a little on what could I set as parameter to m:GetNearestLocations so that I can pass it Integers or Strings

Thanks a lot.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
CharleyXIV
  • 1,590
  • 3
  • 13
  • 24
  • 1
    Don't you need need `at "path/to/some-module.xqy"` as part of the import line? – wst Jul 23 '13 at 16:15
  • Well I'm not sure what it does. Would I need to download the module to "path/to/some-module.xqy" ? I don't know how to download it if that's the case. – CharleyXIV Jul 23 '13 at 16:17
  • Right, unless there is something special about the math module in xqib, I think you need to actually reference an XQY module to import it. http://www.w3.org/TR/xquery/#doc-xquery-ModuleImport – wst Jul 23 '13 at 16:21
  • 1
    Regarding your last question: integer and strings are both of the root type `xs:anyAtomicType`. However, if you use this you could also pass other types. You might want to check using the `instance of` operator. I would still use another approach: make your general function private and just call it from two other functions, expecting a string or integer as input, respectively. – dirkk Jul 24 '13 at 11:17

1 Answers1

2

Now I want to import a math module so that I can use some of its functions while reading data from a XML file.

That sounds reasonable. So your first task will be to find an implementation of the math module for namespace http://www.w3.org/2005/xpath-functions/math that XQiB / MXQuery can process, install it on your server, and point to it from the module import statement, as shown in the module import example on the XQiB web site.

Or alternatively, you may decide that you can work with the math functions in http://www.zorba-xquery.com/zorba/math-functions, which MXQuery (and thus XQiB) appear to support natively. (I see this in the MXQuery documentation but not in the XQiB documentation, so I guess there is the theoretical possibility that XQiB is using an older version of MXQuery -- but it's more likely that the development team just has better things to do with their time than document modules already documented elsewhere.)

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • What do you mean by install it on your server? I have to install the functions? Because I think that is what's missing in my case. – CharleyXIV Jul 26 '13 at 17:29
  • 1
    By 'your server' I mean the machine which serves your HTML document and mxqueryjs/mxqueryjs.nocache.js over HTTP. By 'install', I mean 'make a copy of the relevant software in an appropriate place on the server'. The XQuery functions you want to run will execute in the client, but yes, you do have to install them on your server, so that the client can load and run them -- just as you had to install mxqueryjs.nocache.js on your server in order to get XQiB to run at all in your browser. (Built-in libraries -- the Zorba math library may be one -- were installed when you installed MXQuery.) – C. M. Sperberg-McQueen Jul 26 '13 at 18:49