xslt - Function that can be use to omit duplicate value on a string -
i ask if there function can use to remove duplicate value inside string separated | simplest possible way. have below example of string
1111-1|1111-1|1111-3|1111-4|1111-5|1111-3
the output i'm expecting is:
1111-1|1111-3|1111-4|1111-5
thanks in advance.
all presented xslt 1.0 solutions far produce wrong result:
1111-1|1111-4|1111-5|1111-3
whereas wanted, correct result is:
1111-1|1111-3|1111-4|1111-5
now, following transformation (no extensions, pure xslt 1.0):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text"/> <xsl:template match="text()" name="distinctsubstrings"> <xsl:param name="ptext" select="."/> <xsl:param name="poutdelim"/> <xsl:param name="pfounddistinctsubs" select="'|'"/> <xsl:param name="pcountdistinct" select="0"/> <xsl:if test="$ptext"> <xsl:variable name="vnextsub" select="substring-before(concat($ptext, '|'), '|')"/> <xsl:variable name="visnewdistinct" select= "not(contains(concat($pfounddistinctsubs, '|'), concat('|', $vnextsub, '|')))"/> <xsl:variable name="vnextdistinct" select= "substring(concat($poutdelim,$vnextsub), 1 div $visnewdistinct)"/> <xsl:value-of select="$vnextdistinct"/> <xsl:variable name="vnewfounddistinctsubs" select="concat($pfounddistinctsubs, $vnextdistinct)"/> <xsl:variable name="vnextoutdelim" select="substring('|', 2 - ($pcountdistinct > 0))"/> <xsl:call-template name="distinctsubstrings"> <xsl:with-param name="ptext" select="substring-after($ptext, '|')"/> <xsl:with-param name="pfounddistinctsubs" select="$vnewfounddistinctsubs"/> <xsl:with-param name="pcountdistinct" select="$pcountdistinct + $visnewdistinct"/> <xsl:with-param name="poutdelim" select="$vnextoutdelim"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>
when applied on xml document (with string value provided string in question):
<t>1111-1|1111-1|1111-3|1111-4|1111-5|1111-3</t>
produces wanted, correct result:
1111-1|1111-3|1111-4|1111-5
explanation:
all found distinct substrings concatenated in parameter
$pfounddistinctsubs
-- whenever next substring delimited input, compare distinct substrings passed in parameter. ensures first in order distinct substring output -- not last in other 2 solutions.we use conditionless value determination, based on fact xslt 1.0 implicitly converts boolean
false()
0
,true()
1
whenever used in context requires numeric value. in particular,substring($x, 1 div true())
equivalentsubstring($x, 1 div 1)
is:substring($x, 1)
, entire string$x
. on other side,substring($x, 1 div false())
equivalentsubstring($x, 1 div 0)
-- is:substring($x, infinity)
, empty string.
to know why avoiding conditionals important: watch pluralsight course:
tactical design patterns in .net: control flow, zoran horvat
Comments
Post a Comment