XSLT Copy all nodes except 1 element

<events> <main> <action>modification</action> <subAction>weights</subAction> </main> </events> <SeriesSet> <Series id="Price_0"> <seriesBodies> <SeriesBody> <DataSeriesBodyType>Do Not Copy</DataSeriesBodyType> </SeriesBody> </SeriesBodies> </Series> </SeriesSet> 

How to copy all xml and exclude DataSeriesBodyType element

+7
source share
2 answers

You just need to use the identity template (as you used), and then use the template corresponding to the DataSeriesBodyType, which does nothing.

The code:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="utf-8" indent="yes"/> <!-- Identity template : copy all text nodes, elements and attributes --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <!-- When matching DataSeriesBodyType: do nothing --> <xsl:template match="DataSeriesBodyType" /> </xsl:stylesheet> 

If you want to normalize the output to remove empty text nodes, add the following template to the previous stylesheet:

 <xsl:template match="text()"> <xsl:value-of select="normalize-space()" /> </xsl:template> 
+17
source

There is also a wonderful entry on XMLPlease.com about these issues. It contains many examples for excluding elements, attributes, renaming elements, etc. Etc.

See the following website: http://www.xmlplease.com/xsltidentity

+4
source

All Articles