Removing blank tags from Plone content with Diazo

I have a Plone website thematic with plone.app.theming. The problem is that the design is pretty strict and does not involve any empty <p> elements or any other meaningless TinyMCE outputs. Such elements violate the intended design. Therefore, I want to remove empty elements from the content. I tried inline xslt (which according to http://diazo.org/advanced.html#inline-xsl-directives should be supported):

 <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 did not help. In fact, it did something strange. The empty p tags that I wanted to get rid of remained unchanged, but some other elements like

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

turned into

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

with a crossed out image. Replacing match="*[… in the second template with match="p[… did not crop the images, but these nasty <p> were still in the output.

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

UPDATE January 31, 2012 Here is the content from which I need to remove p tags:

 <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> 

Diazo conversion 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 result that I get after applying the transformations to the Plone website is completely identical to the input, while I expect to get these three empty <p> tags after opening the <div> to leave.

If I changed the second template to fit all elements, such as match="*… , then the images will be deleted, but empty <p> tags still exist.

+6
source share
2 answers

It works for me. I added an example of using xpath in Dimitre in the quotes content rule in https://github.com/plone/diazo/commit/94ddff7117d25d3a8a89457eeb272b5500ec21c5 , but it also works as an equivalent xsl: template. The example comes down to the basics, but it also works with the full contents of the example in the question.

+2
source

Just use :

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

Full conversion :

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

when this conversion is applied to this XML document :

 <div> <p/> <p> </p> <p><img src="..."/></p> <img src="..."/> </div> 

required, the correct result is obtained :

 <div> <p> <img src="..."/> </p> <img src="..."/> </div> 
+3
source

Source: https://habr.com/ru/post/907312/


All Articles