home contribute faq download

FunctX XSLT Functions


The distinct XML nodes in a sequence (by node identity)



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

$nodes node()* the node sequence
return value node()*

XSLT Function Declaration

See XQuery definition.
<xsl:function name="functx:distinct-nodes" as="node()*"
  <xsl:param name="nodes" as="node()*"/>

  <xsl:sequence select="
    for $seq in (1 to count($nodes))
    return $nodes[$seq][not(functx:is-node-in-sequence(
                                .,$nodes[position() &lt; $seq]))]



<xsl:variable name="in-xml" as="item()*">
XPath ExampleResultsExplanation
     ($in-xml/child, $in-xml/*) )
The two child elements that contain 3 have distinct identities, even if they contain the same content.
     ($in-xml/child[3], $in-xml/*) )
The 3 appears first because it is first in the input sequence.

Depends On

functx:is-node-in-sequenceWhether an XML node is in a sequence, based on node identity

See Also

functx:distinct-deepThe XML nodes with distinct values, taking into account attributes and descendants
fn:distinct-valuesThe distinct atomic values in a sequence


Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Datypic XSLT Services

Recommended Reading: