本文介绍了从返回 XQuery 中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XQuery 是:

My XQuery is:

declare namespace xsd="http://www.w3.org/2001/XMLSchema";
for $schema in xsd:schema
for $nodes in $schema//*,
    $attr in $nodes/xsd:element/@name
where fn:contains($attr,'city')
return $attr

返回:name="city" name="city" name="city" name="city" name="city"

当我添加不同的值时:

declare namespace xsd="http://www.w3.org/2001/XMLSchema";
for $schema in xsd:schema
for $nodes in $schema//*,
    $attr in $nodes/xsd:element/@name
where fn:contains($attr,'city')
return distinct-values($attr)

返回:city city city city

我只需要一个城市",我该怎么做?

I need only one "city", how can I do it ?

推荐答案

您需要将 distinct-values 函数应用于整个结果(即,不是每个结果项):

You need to apply the distinct-values function on the whole result (i. e., not to each single result item):

declare namespace xsd="http://www.w3.org/2001/XMLSchema";
distinct-values(
  for $schema in xsd:schema
  for $nodes in $schema//*,
      $attr in $nodes/xsd:element/@name
  where fn:contains($attr,'city')
  return $attr
)

查询也可以写成单个 XPath 表达式:

The query can also be written as a single XPath expression:

distinct-values(//xs:element/@name[contains(., 'city')])

这篇关于从返回 XQuery 中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:37