passing parameter for external variable
Added by Anonymous almost 20 years ago
Legacy ID: #2942430 Legacy Poster: amitabh ojha (amitabhojha)
Thanks for your reply based on which I have since successfully tried out some experiments, in each case using external variable of atomic type i.e a numeric (integer or double) or String. While working with the Saxon Java Xquery API, I supplied the xquery external parameters as java command line arguments i.e as args [0], args [1] and so on. Now if the xquery expects an external String parameter, then the relevant String object out of the args [0], args [1] etc can be directly supplied as second argument to the setParameter method. However if the xquery expects the external parameter to be a numeric, then it cannot be passed to the setParameter method as a String object. In that case, an appropriate object type say Java Double/ Integer or an equivalent object type from Saxon’s Value class (after carrying out this necessary transformation in the user java code) is to be supplied to the setParameter method. So, here the user java code must include a few lines of extra code to convert strings to another appropriate class, if required. Or else if we allow the parameter to get passed to setParameter method as a string, then in that case the xquery must include extra code to cast that string into the appropriate type which the xquery requires/ expects as external variable. As an example, if the user code supplies a “Double” type object to setParameter method, then saxon7.jar would take care of delivering that as a “double” numeric if the xquery is expecting a “double”. However, the conversion of the external variable (always received prima-facie as a String) to say Double (before its being supplied as second argument to setParameter method) is the responsibility of the user java code or user xquery code. On the other hand, when one executes the Xquery directly from command line (i.e not from Saxon Java API), then the parameter value supplied at command line will always get passed to the xquery as a string. And if the external parameter is expected to be a numeric (and not a String) and finds use in an arithmetic expression, then the user xquery code should include extra lines of code to actually cast the external parameter, prima-facie received as a string, into the type that the arithmetic expression wants. Or is there a way to make it known that the supplied string value at command line is ultimately to be treated as a integer or a double. I will feel grateful if you would kindly confirm if my understanding as stated above, is correct. Amitabh Ojha
Replies (1)
RE: passing parameter for external variable - Added by Anonymous almost 20 years ago
Legacy ID: #2942639 Legacy Poster: Michael Kay (mhkay)
The rules are not quite how you describe (I had to look at the code in some detail to see how they actually work). When you invoke a query from the command line, this is equivalent to supplying a parameter value that's an instance of UntypedAtomicValue, and Saxon will attempt to convert this to the declared type of the external variable using the XPath casting rules. In fact the cast will also be done if you supply say a StringValue or a String. I had been under the impression that Saxon would only do the conversions allowed by the function calling rules (for example UntypedAtomic to Double, but not String to Double) but it seems that it actually attempts a more forceful conversion than this. It's certainly doing a stronger conversion that it would for a non-external variable, where XQuery requires an exact correspondence between the actual type of the value returned by the initializing expression and the declared type of the variable. Michael Kay
Please register to reply