首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从返回XQuery中删除重复项

从返回XQuery中删除重复项
EN

Stack Overflow用户
提问于 2013-06-01 22:32:39
回答 3查看 9.4K关注 0票数 5

我的XQuery是:

代码语言:javascript
复制
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"

当我添加distinct-values时:

代码语言:javascript
复制
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 city

我只需要一个“城市”,我怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-01 23:12:00

您需要在整个结果上应用distinct-values函数(即 ,而不是每个单独的结果项):

代码语言:javascript
复制
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表达式:

代码语言:javascript
复制
distinct-values(//xs:element/@name[contains(., 'city')])
票数 8
EN

Stack Overflow用户

发布于 2013-06-01 23:08:04

使用group by。您的查询多次返回city,因为在( for循环的)每次迭代中,$attr中只有一个这样的元素。因此,您正在对单个元素执行distinct-values,但您会多次执行此操作。

代码语言:javascript
复制
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')
group by $attr
return $attr
票数 4
EN

Stack Overflow用户

发布于 2013-06-01 23:14:10

这项工作

代码语言:javascript
复制
distinct-values(for $schema in xsd:schema
for $nodes in $schema//*,
    $attr in $nodes/xsd:element/@name
where fn:contains($attr,'city')
return distinct-values($attr))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16873753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档