Search code examples
xslt-1.0xsl-fo

How to convert some text in paragraph into bold xsl fo?


This is how my xml looks like:

xml:

<Documents>
 <Document>
<Note>
  <Header>
    <HeaderText> &lt;b&gt;Need Help?&lt;/b&gt; Contact Our Customer Happiness Team
     by phone  &lt;b&gt;0345 00002662&lt;/b&gt;
     Mon-Fri 9am-7pm
   </HeaderText>
  </Header>
  </Note>
 </Document>
   </Documents>

I want to convert some text in HeaderText in bold. E.g Need Help? Contact Our Customer Happiness Team by phone 0345 00002662 Mon-Fri 9am-7pm

Xslt:

 <fo:table-header text-align="left" border-width="0mm">
    <fo:table-row margin-left="1cm" font-family="Avenir" font-size="14pt">
      <fo:table-cell>
        <fo:block padding-top="0cm">
          <xsl:value-of select="HeaderText" />
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </fo:table-header>
 

Solution

  • The &lt;b&gt;Need Help?&lt;/b&gt; is just text to the XSLT processor and the XSL-FO formatter.

    This is similar to XSLT - How to treat inline/escaped XML within a node as nested nodes. The advice there doesn't get beyond doing it in two passes or using XSLT 2.0 or 3.0.

    If &lt;b&gt; is the only non-element that you need to convert into real markup, then you can do it with a recursive template:

    <xsl:template match="text()[contains(., '&lt;b>')]"
                  name="unescape-bold">
      <xsl:param name="text" select="." />
    
      <xsl:choose>
        <xsl:when test="not(contains($text, '&lt;b>'))">
          <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="substring-before($text, '&lt;b>')" />
          <fo:inline font-weight="bold">
            <xsl:value-of
                select="substring-before(substring-after($text, '&lt;b>'),
                                         '&lt;/b>')" />
          </fo:inline>
          <xsl:call-template name="unescape-bold">
            <xsl:with-param name="text"
                            select="substring-after($text, '&lt;/b>')" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    For your sample, this gives:

     <fo:inline font-weight="bold">Need Help?</fo:inline> Contact Our Customer Happiness Team
         by phone  <fo:inline font-weight="bold">0345 00002662</fo:inline>
         Mon-Fri 9am-7pm