首页 > 解决方案 > 如何比较xpath中的元素位置

问题描述

我正在尝试比较客户帐户值以仅显示不同的值并忽略 XPath 中的重复项:

XML 代码:

<info> 
  <Customer CustAccount="1"/>
  <Customer CustAccount="2"/>
  <Customer CustAccount="2"/>
  <Customer CustAccount="3"/> 
</info>

结果应比较客户 1/2/3 并显示:

customer 1
customer 2
customer 3

标签: xpath

解决方案


您可以使用 XPath-2.0 表达式来实现这一点

for $c in distinct-values(/info/Customer/@CustAccount) return concat('customer ',$c,'&#xa;')

输出是:

customer 1
customer 2
customer 3

如果您不喜欢换行符,请&#xa;从表达式中删除。
没有纯粹的 XPath-1.0 表达式可以实现这一点;如果 XPath-2.0 不可用,您只能使用 XSLT-1.0 执行此操作。


推荐阅读