我想在XSLT中使用XPath来选择具有基于属性值的条件的节点 .
为了说明我的问题,我有一个简短的XML实例,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<elementA fID="2013_4_20150722_0" dateTime="2015-07-13T01:04:20+02:00"/>
<elementA fID="2013_4_20150721_0" dateTime="2015-07-13T01:04:20+02:00"/>
<elementA fID="2013_4_20150721_0" dateTime="2015-07-20T14:14:22+02:00"/>
</root>
我想在以下条件下选择所有 elementA
节点:
-
属性
fID
是唯一的 -
如果有多个具有相同
fID
属性值的elementA
节点,则仅选择具有最新dateTime
的节点 .
所以在我的例子中我想选择第一个和第三个 elementA
.
如何在XSLT 2.0中使用XPath 2.0实现这一目标?
2 回答
Here is a pure, single and efficient (no sorting) XPath 2.0 expression ,选择想要的元素:
Here is a proof ,其中XSLT仅用于将表达式的计算结果复制到输出中:
When the above transformation is applied on this source XML document :
the wanted, correct result is produced :
Note on efficiency :
此XPath表达式仅使用
max()
函数,即我会在XSLT 2.0中进行分组和排序,如果你想在XPath中使用它,你可以编写一个包含功能的用户定义函数:
在线示例位于http://xsltransform.net/jyH9rNb .