XPath/XSLT Remove Empty Tags

I would like to remove tags which contain only whitespace/newline/tab chars, as below:

<p>    </p>

How would you do this using xpath functions and xslt templates?


This transformation (overriding the identity rule):

<xsl:stylesheet version="1.0"
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
       <xsl:apply-templates select="node()|@*"/>

 <xsl:template match="*[not(*) and not(text()[normalize-space()])]"/>

when applied to the following XML document:

 <p>  </p>

correctly produces the wanted result:


Remember: Using and overriding the identity rule/template is the most fundamental and powerful XSLT design pattern. It is the right choice for a variety of problems where most of the nodes are to be copied unchanged and only some specific nodes need be altered, deleted, renamed, ..., etc.

Note: @Abel in his comment recommends that some bits of this solution need to be further explained:

For the uninitiated or curious: not(*) means: not having an child element; not(text()[normalize-space()]) means: not having a text-node with non - white-space-only text.

Need Your Help

Output every file status in a git tree


I'm trying to output the name and status of every file (tracked or untracked) in a git working tree - preferably in an machine-readable format - but I cannot find any git command to do this.

Why is Java automatically casting from String to Integer when reading LinkedHashMap

java string integer classcastexception linkedhashmap

My Java application is displaying some odd behaviour, and I'm having trouble finding a solution.