There is your solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="@* | text()"> <xsl:copy /> </xsl:template> <xsl:template match="table | tr | td"> <xsl:variable name="content"> <xsl:apply-templates select="node()" /> </xsl:variable> <xsl:if test="count($content/node()) > 0"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="$content" /> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet>
The idea is simple. First, I will make a transformation for my descendants, and then see if anyone remains. If so, I will copy myself and the result of the conversion.
If you want to keep the table structure and delete only empty rows - <tr> elements containing only empty <td> elements, than just create a similar template for <tr> with a different condition and ignore the <td> elements.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="@* | text()"> <xsl:copy /> </xsl:template> <xsl:template match="table"> <xsl:variable name="content"> <xsl:apply-templates select="node()" /> </xsl:variable> <xsl:if test="count($content/node()) > 0"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="$content" /> </xsl:copy> </xsl:if> </xsl:template> <xsl:template match="tr"> <xsl:variable name="content"> <xsl:apply-templates select="node()" /> </xsl:variable> <xsl:variable name="cellCount"> <xsl:value-of select="count($content/td[node()])" /> </xsl:variable> <xsl:variable name="elementCount"> <xsl:value-of select="count($content/node()[name() != 'td'])" /> </xsl:variable> <xsl:if test="$cellCount > 0 or $elementCount > 0"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="$content" /> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet>
Well, actually the last if should be like this:
<xsl:choose> <xsl:when test="$cellCount > 0"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="$content" /> </xsl:copy> </xsl:when> <xsl:when test="$elementCount > 0"> <xsl:copy> <xsl:apply-templates select="@*" /> <xsl:copy-of select="$content/node()[name() != 'td']" /> </xsl:copy> </xsl:when> </xsl:choose>
This is because the <tr> contains empty <td> elements and other elements. Then you want to remove the <td> and leave only the rest.
source share