Database Reference
In-Depth Information
The code for the weather.xq scheduled weather web service query is listed in
Example 16-9 .
Example 16-9. The weather web service scheduled query (weather.xq)
xquery version "3.0";
import module namespace http = "http://expath.org/ns/http-client";
import module namespace util = "http://exist-db.org/xquery/util";
import module namespace xmldb = "http://exist-db.org/xquery/xmldb";
declare namespace wsx = "http://www.webserviceX.NET";
(: Configuration :)
declare variable $local:city external;
declare variable $local:country external;
declare variable $local:weather-collection external;
let $webservice := "http://www.webservicex.net/globalweather.asmx/GetWeather",
$url := $webservice || "?CityName=" || encode-for-uri($local:city) ||
"&CountryName=" || encode-for-uri($local:country) ,
$result := http:send-request(<http:request href="{$url}" method="get"/>)
return
let $doc := if($result[1]/@status eq "200" and $result[2]/wsx:string) then
(: reconstruct XML, the webservice provides it as a string
for some reason! :)
util:parse($result[2]/wsx:string/text())
else
(: record failure :)
<failed at="{current-dateTime()}">{$result}</failed>
return
let $stored := xmldb:store($local:weather-collection, (), $doc)
return
(: log that we ran! :)
util:log("debug", "Stored hourly weather to: " || $stored)
These externally bound variables are filled by the parameters to the scheduled
job configuration.
We prepare the full URL for the weather web service call.
We make a call to the weather web service using the EXPath HTTP Client
extension module (see http ).
Search WWH ::




Custom Search