This example application displays the departure information for a specific station and provides the feature to filter for a special bus or tram line.
Retrieving the Data
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:
The parameters have the following meanings:
- f The unique station ID of Nydalen.
- t The hour in 0-23 format.
- m The minute in 0-59 format.
- d The date in dd.mm.yyyy format.
- start Not interesting for our use but should be passed.
As a result we get the following document:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<template>
<do type="prev" name="b" label="Tilbake"><prev/></do>
<do type="options" name="n" label="Nytt sk"><go href="velkommen.wml"/></do>
</template>
<card id="Liste" title="Trafikanten">
<p>
<small>
Fra Nydalen [T-bane]:<br />
<a href="Rute.asp?d=3011030&t=21832&l=4&Start=1">13.00</a>
4 Bergkrystallen [T-bane]<br />
<a href="Rute.asp?d=3012585&t=22543&l=6&Start=1">13.05</a>
6 sjordet<br />
<a href="Rute.asp?d=3011730&t=22264&l=5&Start=1">13.09</a>
5 Vestli [T-bane]<br />
<a href="Rute.asp?d=3012120&t=22080&l=5&Start=1">13.13</a>
5 Storo [T-bane]<br />
<a href="Rute.asp?d=3011030&t=21831&l=4&Start=1">13.15</a>
4 Bergkrystallen [T-bane]<br />
<a href="Rute.asp?d=3012585&t=22542&l=6&Start=1">13.20</a>
6 sjordet<br />
<a href="Rute.asp?d=3011730&t=22263&l=5&Start=1">13.24</a>
5 Vestli [T-bane]<br />
<a href="Rute.asp?d=3012120&t=22079&l=5&Start=1">13.28</a>
5 Storo [T-bane]<br />
<a href="Rute.asp?d=3011030&t=21830&l=4&Start=1">13.30</a>
4 Bergkrystallen [T-bane]<br />
<a href="Rute.asp?d=3012585&t=22541&l=6&Start=1">13.35</a>
6 sjordet<br />
<br />
<a title="Neste 10" href="F.asp?f=03012130&t=13&m=35&d=14.11.2008&Start=11">Neste 10 avganger</a>
<br/>
<a href="F.asp?f=03012130&t=14&d=14.11.2008&Start=1">Neste timeintervall</a>
<br/>
<a href="F.asp?f=03012130&t=12&d=14.11.2008&Start=1">Forrige timeintervall</a>
<br/>
<a href="Velkommen.wml">"Nytt sk"</a>
<br/>
</small>
</p>
</card>
</wml>
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:
TimeInformation::List TimeQuery::queryInternal(const QString &stationId, const QDateTime &dateTime)
{
const QString timesQueryUrl = QString("doc('http:
.arg(stationId)
.arg(dateTime.time().hour())
.arg(dateTime.time().minute())
.arg(dateTime.toString("dd.MM.yyyy"));
const QString directionsQueryUrl = QString("doc('http:
.arg(stationId)
.arg(dateTime.time().hour())
.arg(dateTime.time().minute())
.arg(dateTime.toString("dd.MM.yyyy"));
QStringList times;
QStringList directions;
QXmlQuery query;
query.setQuery(timesQueryUrl);
query.evaluateTo(×);
query.setQuery(directionsQueryUrl);
query.evaluateTo(&directions);
if (times.count() != directions.count())
return TimeInformation::List();
TimeInformation::List information;
for (int i = 0; i < times.count(); ++i)
information.append(TimeInformation(times.at(i).simplified(), directions.at(i).simplified()));
return information;
}
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:
will return the following document:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<template>
<do type="prev" name="b" label="Tilbake"><prev/></do>
<do type="options" label="Nytt sk"><go href="velkommen.wml"/></do>
</template>
<card id="Liste" title="Trafikanten">
<p>
<small>
Velg stoppsted: <br />
<a title="Velg" href="DateLink.asp?fra=05280320">Nydalen (stre Toten) (-T)</a><br />
<a title="Velg" href="DateLink.asp?fra=03012126">Nydalen st. (i Store ringvei) (OSL)</a><br />
<a title="Velg" href="DateLink.asp?fra=03012131">Nydalen T [buss] (OSL)</a><br />
<a title="Velg" href="DateLink.asp?fra=03012130">Nydalen [T-bane] (OSL)</a><br />
<a title="Velg" href="DateLink.asp?fra=03012125">Nydalen [tog] (OSL)</a><br />
<br/>
<a title="Nytt sk" href="Velkommen.wml">"Nytt sk"</a>
<br/>
</small>
</p>
</card>
</wml>
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:
StationInformation::List StationQuery::query(const QString &name)
{
const QString stationIdQueryUrl = QString("doc(concat('http:
const QString stationNameQueryUrl = QString("doc(concat('http:
QStringList stationIds;
QStringList stationNames;
QXmlQuery query;
query.bindVariable("station", QVariant(QString::fromLatin1(QUrl::toPercentEncoding(name))));
query.setQuery(stationIdQueryUrl);
query.evaluateTo(&stationIds);
query.bindVariable("station", QVariant(QString::fromLatin1(QUrl::toPercentEncoding(name))));
query.setQuery(stationNameQueryUrl);
query.evaluateTo(&stationNames);
if (stationIds.count() != stationNames.count())
return StationInformation::List();
StationInformation::List information;
for (int i = 0; i < stationIds.count(); ++i)
information.append(StationInformation(stationIds.at(i), stationNames.at(i)));
return information;
}
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 Widgets Examples.