从返回XQuery中删除重复项

Pet*_*nok 2 xml xquery basex xquery-3.0

我的XQuery是:

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
Run Code Online (Sandbox Code Playgroud)

返回: 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)
Run Code Online (Sandbox Code Playgroud)

返回: city city city city city

我只需要返回一个"城市",我该怎么办呢?

Chr*_*rün 7

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

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
)
Run Code Online (Sandbox Code Playgroud)

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

distinct-values(//xs:element/@name[contains(., 'city')])
Run Code Online (Sandbox Code Playgroud)