I would like to ask if there is a function that can be use to to remove a duplicate value inside a string separated by | simplest possible way. I have below example of the string
1111-1|1111-1|1111-3|1111-4|1111-5|1111-3
the output that I'm expecting is:
1111-1|1111-3|1111-4|1111-5
Thanks in advance.
To do this in pure XSLT 1.0, with no extension functions, you will need to use a recursive named template:
Full demo: http://xsltransform.net/ncdD7mM
Added:
The above method outputs the last occurrence of each value in the list, because that's the simplest way to remove the duplicates.
The side effect of this is that the original order of the values is not preserved. Or - more correctly - it is the reverse order that is being preserved.
I would not think preserving the original forward order is of any importance here. But in case you do need it, it could be done this way (which I believe is much easier to follow than the suggested alternative):
Assuming that you can use XSLT 2.0, and assuming that the input looks like
you could use the
distinct-values
andtokenize
functions:And the result will be
All presented XSLT 1.0 solutions so far produce the wrong result:
whereas the wanted, correct result is:
Now, the following transformation (no extensions, pure XSLT 1.0):
when applied on this XML document (with string value the provided string in the question):
produces the wanted, correct result:
Explanation:
All found distinct substrings are concatenated in the parameter
$pFoundDistinctSubs
-- whenever we get the next substring from the delimited input, we compare it to the distinct substrings passed in this parameter. This ensures that the first in order distinct substring will be output -- not the last as in the other two solutions.We use conditionless value determination, based on the fact that XSLT 1.0 implicitly converts a Boolean
false()
to0
andtrue()
to1
whenever it is used in a context that requires a numeric value. In particular,substring($x, 1 div true())
is equivalent tosubstring($x, 1 div 1)
that is:substring($x, 1)
and this is the entire string$x
. On the other side,substring($x, 1 div false())
is equivalent tosubstring($x, 1 div 0)
-- that is:substring($x, Infinity)
and this is the empty string.To know why avoiding conditionals is important: watch this Pluralsight course:
Tactical Design Patterns in .NET: Control Flow, by Zoran Horvat
I have adapted a stylesheet below from (XSLT 1.0 How to get distinct values)
with a sample input of:
and the output is
**** EDIT ****
per Michael's comment below, here is the revised stylesheet which uses a saxon extension:
given an input of:
it outputs
and with this input:
the output is