xml - XSLT Select non duplicate node values -
i trying non duplicate node values list. have tried many suggestions nothing quite worked me. source xml
<records> <record> <files> <file> <name>a</name> </file> <file> <name>b</name> </file> <file> <name>b</name> </file> </files> </record> <record> <files> <file> <name>a</name> </file> <file> <name>b</name> </file> <file> <name>c</name> </file> <file> <name>c</name> </file> </files> </record> </records>
output looking should looks text this
a,b | a,b,c
first comma separated set first record , second set (after "|") second record. (delimiter placement, spaces etc... not issue, removal of duplicates)
code have looks this
<xsl:key name="nameid" match="name" use="." /> <xsl:for-each select="records/record"> <xsl:call-template name="doeach_record"/> <xsl:text>|</xsl:text> </xsl:for-each> <xsl:template name="doeach_record"> <xsl:for-each select="files/file"> <xsl:if test="generate-id(name) = generate-id(key('nameid', name)[1])"> <xsl:value-of select="name"/> </xsl:if> <xsl:text>,</xsl:text> </xsl:for-each> </xsl:template>
it remove duplicates first record, second record not selecting name values found in first record. getting output this
a,b | ,,c
there xslt 2.0 since 2007 various implementations saxon 9, xmlprime, altova use <xsl:template match="record"><xsl:value-of select="distinct-values(files/file/name)" separator=","/></xsl:template>
. if restricted xslt 1.0 need define key takes generated-id of ancestor record
name
e.g. <xsl:key name="unique" match="file/name" use="concat(generate-id(ancestor::record), '|', .)"/>
, can use usual xslt 1.0 key based muenchian grouping/distinct value approach.
Comments
Post a Comment