找到最小/最大同步dateTime节点的最短XPath [英] Shortest XPath to find minimum/maximum of sibling dateTime nodes

查看:95
本文介绍了找到最小/最大同步dateTime节点的最短XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的xml:

I have the following simple xml:

<root>
 <item>
  <d>2002-05-30T09:00:00</d>
 </item>
 <item>
  <d>2005-05-30T09:00:00</d>
 </item>
 <item>
  <d>2003-05-30T09:00:00</d>
 </item>
</root>

现在我想使用XPath找到最小或最大的dateTime节点。

Now I want to find the minimum or maximum dateTime node using XPath.

我的解决方案是:

/root/item[not(number(translate(./d, 'TZ:-', '.')) <= number(translate(following-sibling::item, 'TZ:-', '.')))][not(number(translate(./d, 'TZ:-', '.')) <= number(translate(preceding-sibling::item, 'TZ:-', '.')))][1]/d

它的工作原理是丑陋的,而且效率不高。基本上它将dateTime转换成一个数字,然后将它们相互比较。我从这里修改了这一点。

It works but is is ugly as hell and not very efficient. Basically it converts the dateTime to a number and then compares them with each other. I adapted this from here.

最好的方法是什么?

欢呼

neo

推荐答案

如果您不能提前知道项目的数量

You could not in XPath 1.0 if you will not know in advance the number of item because every function wich has a no node-set argument cast its argument taking the first node in node-set, and order comparison operator doesn't work with strings.

在XPath 2.0中,您可以使用:

In XPath 2.0 you could use:

max(/root/item/d/xs:dateTime(.))

这篇关于找到最小/最大同步dateTime节点的最短XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆