2013-11-26 73 views
0

我有以下XQuery函數。如何在xquery中打印唯一值

declare function local:format($input as element() *) as element() *{ 

for $v in 

$input 
return if 
(((for $x in $input return $x)/@one=$v/@two) and ((for $y in $input return $y)/@two=$v/@one)) 
     then 
     ( 





     ) 

     else {$v} 


}; 

輸入上面的功能是:

** <pair two="IN011" one="IN007"> David Emma </pair> ** 


    ** <pair two="IN007" one="IN011"> Emma David </pair> ** 

但我想輸出是:

** <pair two="IN011" one="IN007"> David Emma </pair> ** 

要不然:

** <pair two="IN007" one="IN011"> Emma David </pair> ** 

也就是說,我希望它只能打印一次。

上述功能的功能應該是這樣的。但是,這是不完整的。我儘可能地嘗試。請幫忙讓我上面

回答

1

提到的輸出你想要做的基於標準化的字符串被標準化值在源序列,並返回不同的值是什麼:

declare function local:normalize(
    $label as xs:string 
) as xs:string 
{ 
    string-join(
    for $t in tokenize($label, '\s+') 
    order by $t 
    return $t) 
}; 

let $pairs := (<pair two="IN011" one="IN007"> David Emma </pair>, 
    <pair two="IN007" one="IN011"> Emma David </pair>) 
let $pairs-normalized := 
    for $p in $pairs 
    return element pair { 
    $p/@*, 
    attribute hash { local:normalize($p) }, 
    $p/node() 
    } 
return $pairs-normalized[index-of($pairs-normalized/@hash, @hash)[1]] 

=><pair two="IN011" one="IN007" hash="DavidEmma"> David Emma </pair>

做的另一種方法是使用distinct-values

for $d in distinct-values($pairs-normalized/@hash) 
return ($pairs-normalized[@hash = $d])[1]