[英]Xpath to find the element with the maximum value for a given child
这是XML:
<root>
<ACTS>
<ACT>Play</ACT>
<A>
<Day>1</Day>
<time>Fri Feb 28 13:21:42 IST 2014</time>
</A>
<A>
<Day>2</Day>
<time>Fri Feb 28 13:21:43 IST 2014</time>
</A>
</ACTS>
<ACTS>
<ACT>Study</ACT>
<A>
<Day>1</Day>
<time>Fri Feb 28 13:21:42 IST 2014</time>
</A>
<A>
<Day>2</Day>
<time>Fri Feb 28 13:21:43 IST 2014</time>
</A>
<A>
<Day>3</Day>
<time>Fri Feb 28 13:21:43 IST 2014</time>
</A>
</ACTS>
</root>
如何找到Day
=最大值的ACT
= 'Study'
的元素A
及其子元素? 这是Java程序:
String xp = "//ACTS[ACT='Study']/A[not(Day < //ACTS[ACT='Study']/A/Day)]";
XPathExpression expr = xpath.compile(xp);
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
if (null != nl && null != nl.item(0)) {
String abc = nl.item(0).getChildNodes().item(0).getNodeValue();
System.out.println(abc);
}
注意:通过此XPath,我能够获得Day
的最大值
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
if (null != nl && null != nl.item(0)) {
maxAttemptNumber = nl.item(0).getTextContent();
}
但需要帮助以获取元素A及其子元素。
如果您使用Java,则可以像XPath 2.0或XPath 1.0一样轻松使用XQuery(例如,使用Saxon库)。 答案是
(for $a in //ACTS[ACT="Study"]/A
order by number($a/Day))[last()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.