Reputation: 10397
I have the following XML, returned from a web service (5 extra points for guessing the movie):
<ArrayOfSub xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Sub>
<SubId>862</SubId>
<SubStreamId>1</SubStreamId>
<SubTimeStart>00:01:04.4450000</SubTimeStart>
<SubTimeEnd>00:01:08.2450000</SubTimeEnd>
<SubText>Wikus van de Merwe
MNU Alien Affairs
</SubText>
</Sub>
<Sub>
<SubId>863</SubId>
<SubStreamId>1</SubStreamId>
<SubTimeStart>00:02:11.3430000</SubTimeStart>
<SubTimeEnd>00:02:14.8430000</SubTimeEnd>
<SubText>Sarah Livingstone
Sociologist, Kempton Park University
</SubText>
</Sub>
</ArrayOfSub>
I need the <SubText>
element for all elements meeting the following conditions:
SubTimeStart < now && SubTimeEnd > now
I'm doing this in Javascript, for a WebOs app on the Palm Pre. I'm not sure exactly what resolution would be appropriate for my prototype.js PeriodicExecuter, but 1/100th of a second seems to be working OK.
What XPath query will return the element representing what's supposed to be on the screen right now?
Upvotes: 0
Views: 272
Reputation: 42227
XPath2 supports dates and times, so if WebOs uses XPath2 you can do something simple like:
//Sub[(SubStartTime < '00:02:13.243') and SubEndTime > '00:02:13.243']
otherwise you can convert the time to a simple integer like so:
//Sub[
(concat(substring(SubTimeStart,1,2), substring(SubTimeStart,4,2), substring(SubTimeStart,7)) < '000213.243')
and
(concat(substring(SubTimeEnd,1,2), substring(SubTimeEnd,4,2), substring(SubTimeEnd,7)) > '000213.243')
]
These queries should select the second item.
Upvotes: 0
Reputation: 57946
Try this:
var district = 9; // hint
var now = "00:00:00.123"; // manual format is an option?
var xpath = "/ArrayOfSub/Sub["+
" SubTimeStart <= " + now + " and " +
" SubTimeEnd >= " + now + "]/SubText"
Upvotes: 1
Reputation: 546035
XPath does have some date handling functions - though there doesn't seem to be any mention of milliseconds in that list.
I guess the query would be something like this:
nowH = 0;
nowM = 2;
nowS = 26;
/ArrayOfSub/sub //linebreaks added for readability
[fn:hours-from-time(SubTimeStart) <= nowH]
[fn:minutes-from-time(SubTimeStart) <= nowM]
[fn:seconds-from-time(SubTimeStart) <= nowS]
[fn:hours-from-time(SubTimeEnd) >= nowH]
[fn:minutes-from-time(SubTimeEnd) >= nowM]
[fn:seconds-from-time(SubTimeEnd) >= nowS]
/SubText
Upvotes: 0