Search code examples
tinymceplonestripdiazo

Stripping empty tags from Plone content with Diazo


I have a Plone site, themed with plone.app.theming. The problem I have is that the design is quite strict and doesn't assume any empty <p> elements or any other nonsense TinyMCE outputs. Such elements break the intended design. So I want to strip the empty elements from the content. I have tried inline xslt (that, according to http://diazo.org/advanced.html#inline-xsl-directives should be supported) like:

<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(text()[normalize-space()])]"/>

But it didn't do the trick. In fact it made something weird. The empty p tags that I wanted to get rid of stayed intact but some other elements like

<a href="mylink"> <img src="../++theme++jarn.com/whatever.png" /></a>

turned into

<a href="mylink"></a>

with the image being striped out. Replacing match="*[… in the second template to match="p[… didn't strip out the images, but those nasty <p> were still in the output.

Any hints on how to get rid of the empty elements using Diazo rules?

UPDATE January 31, 2012 Here is the content from which I need the empty p tags to be stripped off:

<div id="parent-fieldname-text">
<p></p>
<p> </p>
<p> </p>
<p><section id="what-we-do">
<p class="visualClear summary">Not empty Paragraph</p>
<ul class="thumbsList">
    <li><a href="mylink"> <img src="../++theme++jarn.com/whatever.png" /></a></li>
    <li><a href="mylink"> <img src="../++theme++jarn.com/whatever.png" /></a></li>
</ul>
</section></p>
</div>

The Diazo transformation 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">

    <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="p[not(*) and not(normalize-space())]"/>

    <!-- The default theme, used for standard Plone web pages -->
    <theme href="index.html" css:if-content="#visual-portal-wrapper" />

    <replace css:theme-children="div.contentWrapper" css:content-children="#content" />            
</rules>

The output I get after applying the transformations to the Plone site is absolutely identical to the input while I would expect to get those 3 empty <p> tags after opening <div> to go away.

If I change the second template to match all elements like match="*… then the images get stripped out, but the empty <p> tags are still there.


Solution

  • Works for me. I've added an example of using Dimitre's xpath in a drop content rule at https://github.com/plone/diazo/commit/94ddff7117d25d3a8a89457eeb272b5500ec21c5 but it also works as the equivalent xsl:template. The example is pared down to the basics but it works using the complete example content in the question too.