我的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"
当我添加distinct-values时:
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
我只需要一个“城市”,我怎么做呢?
发布于 2013-06-01 23:12:00
您需要在整个结果上应用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')])发布于 2013-06-01 23:08:04
使用group by。您的查询多次返回city,因为在( for循环的)每次迭代中,$attr中只有一个这样的元素。因此,您正在对单个元素执行distinct-values,但您会多次执行此操作。
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发布于 2013-06-01 23:14:10
这项工作
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))https://stackoverflow.com/questions/16873753
复制相似问题