使用 Diazo 从 Plon 内容中剥离空标签

发布于 2025-01-01 04:42:47 字数 2850 浏览 4 评论 0原文

我有一个 Plone 网站,主题为 plone.app.theming。我遇到的问题是设计非常严格,并且不假设任何空

元素或任何其他无意义的 TinyMCE 输出。这些元素破坏了预期的设计。所以我想从内容中删除空元素。我尝试过内联 xslt (根据 http://diazo.org/advanced.html应该支持#inline-xsl-directives),例如:

<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()])]"/>

但它没有达到目的。事实上,这造成了一些奇怪的事情。我想要删除的空 p 标签保持不变,但其他一些元素(

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

变成了这样

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

如图像被条纹化) 。将第二个模板中的 match="*[... 替换为 match="p[... 并没有去除图像,但那些令人讨厌的

仍在输出中。

关于如何使用重氮规则消除空元素有什么提示吗?

更新 2012 年 1 月 31 日 以下是我需要删除空 p 标签的内容:

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

重氮变换规则:

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

将变换应用于 Plone 站点后得到的输出与输入完全相同,而我将期望在打开

后得到这 3 个空的

标签。

如果我更改第二个模板以匹配 match="*... 等所有元素,则图像会被删除,但空的

标签仍然存在。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2025-01-08 04:42:47

只要有这个

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

完整的转换

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

当此转换应用于此 XML 文档时

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

生成所需的正确结果< /强>:

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

Just have this:

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

A complete transformation:

<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 transformation is applied on this XML document:

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

the wanted, correct result is produced:

<div>
   <p>
      <img src="..."/>
   </p>
   <img src="..."/>
</div>
还给你自由 2025-01-08 04:42:47

对我有用。我在 https://github.com/plone/diazo/commit/94ddff7117d25d3a8a89457eeb272b5500ec21c5 但它也可以用作等效的 xsl:template。该示例被简化为基础知识,但它也可以使用问题中的完整示例内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文