Search code examples
xmlxslt-1.0line-breaks

My xslt is not showing the line breaks within a paragarph


My xslt is not displaying the middle line breaks.

Input: This data is fetched from sql server column

 hello

 www.xyz.com

 hello
 line 1

Template:

<xsl:template name="replace">
<xsl:param name="TFooter" />
<xsl:param name="search-string" select="'&#xD;&#xA;'" />
<xsl:if test="contains($TFooter, $search-string)">
  <xsl:value-of select="substring-before($TFooter, $search-string)" />
  <fo:block />
</xsl:if>
 <xsl:if test="contains($TFooter, $search-string)">
  <!-- recursive call -->
  <xsl:call-template name="replace">
    <xsl:with-param name="TFooter" select="substring-after($TFooter, $search-string)" />
  </xsl:call-template>
</xsl:if>
 </xsl:template>

We can call the template from here:

<fo:table-row>
                <fo:table-cell padding-bottom="1mm" padding-left="0.5cm">
                  <fo:block disable-output-escaping="yes" font-size="10pt" text-align="left">
                    <xsl:call-template name="replace">
                      <xsl:with-param name="TFooter" select="FooterText" />
                    </xsl:call-template>
                  </fo:block>
                </fo:table-cell>
              </fo:table-row>

Current output:

hello
www.xyz.com
hello
line 1

Desired Output:

hello

www.xyz.com

hello
line 1

How can i achieve the middle line breaks using xslt 1.0?


Solution

  • I think <xsl:text>&#xD;&#xA;</xsl:text> is missing in template replace

    If your input (the data fetched from sql server column) is in XML as below: (for an example)

    <?xml version="1.0" encoding="UTF-8"?>
    <FooterText>
         hello
    
         www.xyz.com
    
         hello
         line 1
    </FooterText>
    

    Then the code can be rewritten as following:

    <?xml version="1.0" encoding="UTF-8"?>
    <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="/">
        <fo:table-row>
            <fo:table-cell padding-bottom="1mm" padding-left="0.5cm">
                <fo:block disable-output-escaping="yes" font-size="10pt"
                    text-align="left">
                    <xsl:call-template name="replace">
                        <xsl:with-param name="TFooter" select="FooterText" />
                    </xsl:call-template>
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>
    
    <xsl:template name="replace">
        <xsl:param name="TFooter" />
        <xsl:param name="search-string" select="'&#xD;&#xA;'" />
        <xsl:if test="contains($TFooter, $search-string)">
            <xsl:value-of select="substring-before($TFooter, $search-string)" />
            <xsl:text>&#xD;&#xA;</xsl:text>
            <fo:block />
        </xsl:if>
        <xsl:if test="contains($TFooter, $search-string)">
            <!-- recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="TFooter"
                    select="substring-after($TFooter, $search-string)" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    

    https://xsltfiddle.liberty-development.net/bFN1y8X/9