简体   繁体   English

XQuery:比较日期时间与毫秒

[英]XQuery: comparing datetimes with milliseconds

I have a validation module in Python that executes an XQuery on an XML document to check if any <Start> times come after <End> times, and return the count of them. 我在Python中有一个验证模块,该模块对XML文档执行XQuery,以检查在<End>次之后是否有任何<Start> <End>次,并返回它们的计数。 The query is as follows: 查询如下:

    for $d at $count in ./ty:Detections/Detection
        where $d/Start > $d/End
        return $count

Now this works fine and good for all cases except when milliseconds are appended to an End time but not a Start time, eg: 现在,这对所有情况都适用,除了将毫秒添加到“结束时间”而不是“开始时间”之后,它对所有情况都很好,例如:

        <Start>2009-02-23T02:53:14Z</Start>
        <End>2009-02-23T02:53:14.226Z</End>

This always returns True, even though clearly 14 is less than 14.22. 即使很明显14小于14.22,也始终返回True。 If I add a single decimal place to the <Start> time here, it works -- but is there any better solution? 如果我在此处的<Start>时间添加一个小数位,它可以工作-但是有什么更好的解决方案吗?

Presumably there is no schema binding in effect for the Start and End elements. 大概没有对StartEnd元素有效的模式绑定。 In that case the query is doing an xs:untypedAtomic comparison, which effectively compares the string values, where "Z" compares greater than "." 在那种情况下,查询将执行xs:untypedAtomic比较,该比较有效地比较字符串值,其中"Z"比较大于"." .

What you want is an xs:dateTime comparison, so you should compare the element values after casting them to that type. 您想要的是xs:dateTime比较,因此在将元素值转换为该类型后,您应该比较元素值。 The query thus should look like: 因此查询应如下所示:

    for $d at $count in ./ty:Detections/Detection
    where xs:dateTime($d/Start) > xs:dateTime($d/End)
    return $count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM