xslt - Get only elements that are not repeating in an XML -
i have scenario xml doc contains nodes repeating. want rid of such nodes. please note not "removing duplicates". want remove entries of nodes occuring more once.
ex xml
<readuserobsresponse> <userobs> <obsobjectid>1510</obsobjectid> <userobjectid>443</userobjectid> </userobs> <userobs> <obsobjectid>540</obsobjectid> <userobjectid>514</userobjectid> </userobs> <userobs> <obsobjectid>1521</obsobjectid> <userobjectid>514</userobjectid> </userobs> <userobs> <obsobjectid>547</obsobjectid> <userobjectid>544</userobjectid> </userobs> </readuserobsresponse>
desired output : want remove both entries userobjectid 514
<readuserobsresponse> <userobs> <obsobjectid>1510</obsobjectid> <userobjectid>443</userobjectid> </userobs> <userobs> <obsobjectid>547</obsobjectid> <userobjectid>544</userobjectid> </userobs> </readuserobsresponse>
i've done things, not working. idea count nodes userobjectid current value, put in xsl:if
, print nodes. i'm not sure how write snippet.
this need.
it has template userobs
elements checks whether there 1 userobs
child of parent has same value userobjectid
. if entire node copied output.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="/readuserobsresponse"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="userobs"> <xsl:if test="count(../userobs[userobjectid = current()/userobjectid]) = 1"> <xsl:copy-of select="."/> </xsl:if> </xsl:template> </xsl:stylesheet>
output
<?xml version="1.0" encoding="utf-8"?> <readuserobsresponse> <userobs> <obsobjectid>1510</obsobjectid> <userobjectid>443</userobjectid> </userobs> <userobs> <obsobjectid>547</obsobjectid> <userobjectid>544</userobjectid> </userobs> </readuserobsresponse>
Comments
Post a Comment