XQuery/Delivery Status Report

A common task is the need to integrate local data with related data on another site. Although an increasing number of sites provide an RSS feed, it is often necessary to scrape web pages to get the relevant data.

Delivery Status Reporting
One such case is where a company needs to monitor the status of their deliveries which use a courier service.

In this mock example, a company maintains their own records of all deliveries commissioned, the delivery service used and the service's consignment number.. As an XML file, this might look   like;

 Fred FlintstoneCityLink RZL14823 Bill BaileyCityLink RZL14869</Delivery> <Delivery><CustomerName>Jack and Jill</CustomerName><Service>CityExpress</Service> <ConsignmentNo>RXL9999</ConsignmentNo></Delivery> </DeliveryList>

Integrated Report
The following script shows how the local delivery data can be combined with the data for this delivery obtained from the delivery company. In this case, the delivery company City-Line provides a page for each consignment reporting its status.

The script loops over the relevant deliveries and constructs the appropriate URL to read the page for each delivery. The page is input to an HTML-to-XML conversion (used in the Yahoo Weather feed), and then specific elements are retrieved from the HTML to build an extract in XML of the page. This XML data is then combined with the local data to create a combined report.

import module namespace fwiki = "http://www.cems.uwe.ac.uk/xmlwiki" at "../reports/util.xqm"; declare option exist:serialize "method=xhtml media-type=text/html";

declare variable $citylinkURL := "http://www.city-link.co.uk/pod/podfrm.php?JobNo=ZZZZ";

declare function local:get-consignment($consNo) { let $citylinkURL := replace($citylinkURL,"ZZZZ",$consNo) let $page := fwiki:html-to-xml($citylinkURL) return <Consignment> <CustomerReference> {string($page//table[@id="this_table_holds_the_summary_info"]/tr[1]/td[2])} </CustomerReference> <ScheduledDeliveryDate> {string($page//table[@id="this_table_holds_the_summary_info"]/tr[1]/td[4])} </ScheduledDeliveryDate> <DeliveryStatus> {string($page//table[@id="this_table_holds_the_detailed_status_desc"]/tr[1]/td[2])} </DeliveryStatus> </Consignment> };

let $report := <Report> {for $delivery in //Delivery[Service="CityLink"] let $citylink := local:get-consignment($delivery/ConsignmentNo) return <Delivery> {$delivery/*} {$citylink/*} </Delivery> } </Report> return fwiki:element-seq-to-table($report)

Show Report