I have the current XSLT, which I use to remove empty nodes:
. ,,,,.
I need to find a way to also remove nodes with -1 in them
I assume that you want to remove all the "empty nodes".
Processing depends on the definition of "empty node". One reasonable definition in your case: Any element that has no attributes and children, or has no attributes, and has only one child, which is node text with a value of -1 .
Here is a simple solution for this definition.
This conversion is :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="*[not(@*) and not(*) and (not(text()) or .=-1)]"/> </xsl:stylesheet>
when applying the sample to this XML document :
<t> <a>-1</a> <a>2</a> <b><c/></b> <d>-1</d> <d>15</d> <ex="1"/> <f>foo</f> </t>
creates the desired, correct result :
<t> <a>2</a> <b/> <d>15</d> <ex="1"/> <f>foo</f> </t>
Dimitre novatchev
source share