XSLT to remove duplicates completely -
i know there solutions plenty remove duplicates, 1 different. need remove element output if duplicate. input:
<sanctionlist> <row> <personid>1000628</personid> <personid>1000634</personid> <personid>1113918</personid> <personid>1133507</personid> <personid>1113918</personid> </row> </sanctionlist>
output expected:
<sanctionlist> <row> <personid>1000628</personid> <personid>1000634</personid> <personid>1133507</personid> </row> </sanctionlist>
here tried parser returns 1 each of groups. shouldnt return 2 personid 1113918 since appears twice in list?
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" version="2.0"> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="sanctionlist"> <xsl:for-each-group select="row" group-by="personid"> <xsl:text> count </xsl:text> <xsl:value-of select="current-grouping-key()" /> <xsl:text> </xsl:text> <xsl:value-of select="count(current-group())" /> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
thanks kindly!
i know there solutions plenty remove duplicates, 1 different. need remove element output if duplicate
use short , simple transformation (both in xslt 2.0 , xslt 1.0):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="kpersonbyval" match="personid" use="."/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="personid[key('kpersonbyval', .)[2]]"/> </xsl:stylesheet>
when transformation applied on provided xml document:
<sanctionlist> <row> <personid>1000628</personid> <personid>1000634</personid> <personid>1113918</personid> <personid>1133507</personid> <personid>1113918</personid> </row> </sanctionlist>
the wanted, correct result produced:
<sanctionlist> <row> <personid>1000628</personid> <personid>1000634</personid> <personid>1133507</personid> </row> </sanctionlist>
explanation:
- a wellknown design pattern copying existing xml document , deleting/replacing/inserting nodes copy, overriding identity rule.
- in particular case task delete
<personid>
elements. done providing matching template no (empty) body. - the criterion deletion element must have duplicate -- is, @ least 2
<personid>
elements must exist, having same string value. conveniently done using<xsl:key>
declaration ,key()
function elements same string value. - finally, in match pattern of empty (deleting) template check if node-set of equally-valued elements has second element.
note: can learn more <xsl:key>
declaration , key()
function in module 9 of pluralsight training course "xslt 2.0 , 1.0 foundations"
Comments
Post a Comment