Project

Profile

Help

How to build extension function for removing duplicates in XPath search

Added by Michael Staal-Olsen about 4 years ago

I currently maintain a solution that relies on XSLT 1.0 plus some Integrated Extension Functions which our Java solution has added on top of the default functions that come with Saxon out of the box. I want to build an extension function which takes a node-set (or rather, the result set from an XPath query) and returns only distinct values. That is, if for instance the query '/path/to/something/' resulted in either the set of results:

* example1
* example1
* example2

or:

* <element>hello</element>
* <element>hello</element>
* <element>hello222</element>

then the function should make sure to output the same results, but without duplicates. It seems to me that XSLT 2.0 and beyond comes with a distinct-values function, but I would like to omit upgrading from XSLT 1.0 (for now), and I have not had any luck getting to work as expected.

My idea would be to take the Sequence (which comes from the extension function parameter) and then build a new Sequence just with the relevant pieces of the original - but I do not know how to build a Sequence from another sequence. Would it be possible for you to share some thoughts on this?


Replies (3)

Please register to reply

RE: How to build extension function for removing duplicates in XPath search - Added by Michael Staal-Olsen about 4 years ago

It seems that my second example (which I attempted to express as XML elements) have been formatted incorrectly in this interface. I apologize for that.

RE: How to build extension function for removing duplicates in XPath search - Added by Michael Kay about 4 years ago

I can't see any reason why you would prefer to implement your own extension function rather than just calling fn:distinct-values(), but if that's what you want to do, take a look at the open source implementation of fn:distinct-values() in net.sf.saxon.functions.DistinctValues.java and copy the logic.

RE: How to build extension function for removing duplicates in XPath search - Added by Michael Staal-Olsen about 4 years ago

Thank you for the response. Can you say anything regarding how to (in more general terms) to remove parts of a Sequence object? That is, parts that arise in the iterator.

    (1-3/3)

    Please register to reply