Description
The functx:distinct-nodes function returns the distinct nodes (based on node identity) in a sequence. It does this without resorting them in document order (it takes the first occurrence of each distinct node).It is different from the built-in fn:distinct-values function in that it returns nodes rather than atomic values, and it does not take into account the values (content) of the nodes, just their identity.
Note: if you want them to be resorted in document order, or you don't care, you can simply use the expression "$nodes/." (without the quotes), which works because the slash operator removes duplicates and resorts. The "." in the expression returns the node itself.
Arguments and Return Type| Name | Type | Description |
$nodes |
node()* |
the node sequence |
| return value |
node()* |
XQuery Function Declaration| See XSLT definition. | | XQuery Syntax for July 2004 - January 2007 (1.0): |
|---|
declare namespace functx = "http://www.functx.com";
declare function functx:distinct-nodes
( $nodes as node()* ) as node()* {
for $seq in (1 to count($nodes))
return $nodes[$seq][not(functx:is-node-in-sequence(
.,$nodes[position() < $seq]))]
} ; | | XQuery Syntax for May 2003: |
|---|
declare namespace functx = "http://www.functx.com"
define function distinct-nodes
( $nodes as node()* ) as node()* {
for $seq in (1 to fn:count($nodes))
return $nodes[$seq][fn:not(functx:is-node-in-sequence(
.,$nodes[fn:position() < $seq]))]
} |
Exampleslet $in-xml := | <test>
<child>1</child>
<child>2</child>
<child>3</child>
<child>3</child>
</test> | return |
| XQuery Example | Results | Explanation |
|---|
functx:distinct-nodes(
($in-xml/child, $in-xml/*) )
|
<child>1</child>
<child>2</child>
<child>3</child>
<child>3</child>
|
The two child elements that contain 3 have distinct identities, even if they contain the same content. |
functx:distinct-nodes(
($in-xml/child[3], $in-xml/*) )
|
<child>3</child>
<child>1</child>
<child>2</child>
<child>3</child>
|
The 3 appears first because it is first in the input sequence. |
Depends OnSee AlsoHistory |
|