我的 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
返回: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)
返回:城市城市城市城市城市
我只需要一个“城市”,我该怎么做?
最佳答案
您需要对整个结果应用 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
)
查询也可以写成单个 XPath 表达式:
distinct-values(//xs:element/@name[contains(., 'city')])
关于xml - 从返回的 XQuery 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16873753/