Test for text content in XSLT when trying to insert blank lines
A while ago I asked this question: using xslt stylesheet to convert xhtml blank lines to an XSL-FO blank line
However, I now have a similar issue that the fix that was suggested there will not work for.
The previous solution looked like this:
<xsl:template match="html:br[following-sibling::*[1][self::html:br]]">
<fo:block space-after="1em">
<xsl:call-template name="process-common-attributes"/>
</fo:block>
</xsl:template>
<xsl:template match="html:br[preceding-sibling::*[1][self::html:br]]" />
<xsl:template match="html:br">
<fo:block>
<xsl:call-template name="process-common-attributes"/>
</fo:block>
</xsl:template>
but I now have a piece of html looking like this:
<p>text<br />text<br />text<br /><br /><br />text</p>
The difference with the previous post is that here I do not have elements for the text (like span) but am simply alternating pieces of text and br elements. This will incorrectly give positives for all br's in my example. Unfortunately I do not have control over the input HTML.
The ideal solution that I can come up with w开发者_Go百科ould be to insert the fo:block with 1em height only when there was no text between the next br and this one. Does anyone know either how to achieve this or a better solution for this problem (I do not want to replace all br tags with \n and set linefeed-treatment to preserve as this will open a whole new can of worms)
Edit: the desired output would be this:
text
text
text
text
So it should preserve (multiple) linebreaks but not add additional white lines after single <br>
's.
How this could look like in XSL-FO is this (with the initial block with space-before and space-after coming from the transformation of the <p> element)
<fo:block space-after="1em" space-before="1em">text
<fo:block/>text
<fo:block/>text
<fo:block space-after="1em"/>
<fo:block space-after="1em"/>
<fo:block/>text
</fo:block>
I'm open to suggestions though.
This stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="p">
<fo:block space-after="1em" space-before="1em">
<xsl:apply-templates/>
</fo:block>
</xsl:template>
<xsl:template match="br[following-sibling::node()[1]/self::br]">
<fo:block space-after="1em"/>
</xsl:template>
<xsl:template match="br">
<fo:block/>
</xsl:template>
</xsl:stylesheet>
Output:
<fo:block space-after="1em" space-before="1em"
xmlns:fo="http://www.w3.org/1999/XSL/Format">text
<fo:block />text
<fo:block />text
<fo:block space-after="1em" />
<fo:block space-after="1em" />
<fo:block />text
</fo:block>
精彩评论