TrafficInfo ExampleFiles:
Shows how XQuery can be used extract information from WML documents provided by a WAP service. OverviewThe WAP service used in this example is wap.trafikanten.no that is run by the Norwegian governmental agency for public transport in Oslo. The service provides real time information about the departure of busses, trams and undergrounds for every station in the city area. This example application displays the departure information for a specific station and provides the feature to filter for a special bus or tram line. Without the knowledge of XQuery, one would use QNetworkAccessManager to query the WML document from the WAP service and then using the QDom classes or QXmlStreamReader classes to iterate over the document and extract the needed information. However this approach results in a lot of glue code and consumes valuable developer time, so we are looking for something that can access XML documents locally or over the network and extract data according to given filter rules. That's the point where XQuery enters the stage! If we want to know when the underground number 6 in direction Åsjordet is passing the underground station in Nydalen on November 14th 2008 after 1pm, we use the following URL: http://wap.trafikanten.no/F.asp?f=03012130&t=13&m=00&d=14.11.2008&start=1 The parameters have the following meanings: As a result we get the following document: So for every departure we have a <a> tag that contains the time as a text element, and the following text element contains the line number and direction. To encapsulate the XQuery code in the example application, we create a custom TimeQuery class. This provides the queryInternal() function that takes a station ID and date/time as input and returns the list of times and directions: The first lines of this function synthesize the XQuery strings that fetch the document and extract the data. For better readability, two separated queries are used here: the first one fetches the times and the second fetches the line numbers and directions. The doc() XQuery method opens a local or remote XML document and returns it, so the /wml/card/p/small/ statement behind it selects all XML nodes that can be reached by the path, wml → card → p → small. Now we are on the node that contains all the XML nodes we are interested in. In the first query we select all a nodes that have a href attribute starting with the string "Rute" and return the text of these nodes. In the second query we select all text nodes that are children of the small node which start with a number. These two queries are passed to the QXmlQuery instance and are evaluated to string lists. After some sanity checking, we have collected all the information we need. In the section above we have seen that an unique station ID must be passed as an argument to the URL for retrieving the time, so how to find out which is the right station ID to use? The WAP service provides a page for that as well, so the URL http://wap.trafikanten.no/FromLink1.asp?fra=Nydalen will return the following document: The names of the available stations are listed as separate text elements and the station ID is part of the href attribute of the parent a (anchor) element. In our example, the StationQuery class encapsulates the action of querying the stations that match the given name pattern with the following code: Just as in the TimeQuery implementation, the first step is to synthesize the XQuery strings for selecting the station names and the station IDs. As the station name that we pass in the URL will be input from the user, we should protect the XQuery from code injection by using the QXmlQuery::bindVariable() method to do proper quoting of the variable content for us instead of concatenating the two strings manually. So, we define a XQuery $station variable that is bound to the user input. This variable is concatenated inside the XQuery code with the concat method. To extract the station IDs, we select all a elements that have an title attribute with the content "Velg", and from these elements we take the substring of the href attribute that starts at the 18th character. The station name can be extracted a bit more easily by just taking the text elements of the selected elements. After some sanity checks we have all the station IDs and the corresponding names available. The rest of the code in this example is just for representing the time and station information to the user, and uses techniques described in the Widget Examples. |