XPath根据时间值查找元素

时间:2009-10-04 21:45:47

标签: javascript xpath webos

我有一个从Web服务返回的以下XML(用于猜测电影的5个额外点):

<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>

对于满足以下条件的所有元素,我需要<SubText>元素:

SubTimeStart < now && SubTimeEnd > now

我在Javascript中为Palm Pre上的WebO应用程序执行此操作。我不确定究竟什么分辨率适合我的prototype.js PeriodicExecuter,但1/100秒似乎工作正常。

什么XPath查询将返回表示屏幕上当前应该是什么的元素?

3 个答案:

答案 0 :(得分:1)

试试这个:


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"
 

答案 1 :(得分:0)

XPath确实有一些date handling functions - 尽管在该列表中似乎没有提到毫秒。

我猜查询会是这样的:

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

答案 2 :(得分:0)

XPath2支持日期和时间,因此如果WebO使用XPath2,您可以执行以下简单操作:

//Sub[(SubStartTime < '00:02:13.243') and SubEndTime > '00:02:13.243']

否则你可以将时间转换成一个简单的整数:

//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')
]

这些查询应该选择第二项。