xslt - delete consecutive nodes with same name -
i trying use xslt transform following xml:
<level> <nextlevel> <note> text text text </note> </nextlevel> <nextlevel> <abc> </abc> <note>bla bla bla </note> <note>bla bla bla bla bla</note> <xyz> </xyz> </nextlevel> <nextlevel> <note> text text text </note> </nextlevel> </level>
i want remove duplicate nodes "note" when appears twice consecutively. output should like:
<level> <nextlevel> <note> text text text </note> </nextlevel> <nextlevel> <abc> </abc> <xyz> </xyz> </nextlevel> <nextlevel> <note> text text text </note> </nextlevel> </level>
i using following xslt:
<xsl:key name="dup" match="note" use="concat(generate-id(..), '|', name())"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="note[not(generate-id() = generate-id(key('dup', concat(generate-id(..), '|', .))[1]))]"/>
however, nodes name "note" being deleted, wrong xslt?
try using avoid repetition of consecutive node of same name same value
<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[not(*)][name() = preceding-sibling::*[1]/name()][@value = preceding-sibling::*[1]/@value]" />