Drop empty tags

This recipe demonstrates dropping of empty paragraph tags, including those that contain only whitespace or a single non-breaking space.

Rules

<?xml version="1.0" encoding="UTF-8"?>
<rules
    xmlns="http://namespaces.plone.org/diazo"
    xmlns:css="http://namespaces.plone.org/diazo/css"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- indent and strip space for pretty output -->
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- TinyMCE empty paras include a non breaking space -->
    <drop content="p[not(*) and (not(normalize-space()) or text() = '&#160;')]"/>

    <replace css:theme="#target" css:content="#content" />
</rules>

Theme

<div id="target">
    <div>Content</div>
</div>

Content

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="content">
<p></p>
<p> </p>
<p>&nbsp;</p>
<p> </p>
<p>Not empty paragraph text</p>
<p><span>Not empty paragraph element</span>
</div>
</body>
</html>

Output

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <div id="content">
      <p>Not empty paragraph text</p>
      <p>
        <span>Not empty paragraph element</span>
      </p>
    </div>
  </body>
</html>