Search code examples
xsltxsl-fo

How do I translate <b> tags inside data with xsl-fo


I have report that I must convert to PDF using xsl-fo from xml data coming from a MySQL database.

The xml structure that I am working on has already been used to create a HTML report.

There are certain fields that already have html tags inside that I was able to use in the HTML report by adding disable-output-escaping="yes" to my xsl:value-of's statement.

How do I do the similar operation in xsl-fo? Is there a way I can change the tags to be fo:inline? Or perhaps something I can change in the database output that will be the equivalent PDF version of bolding?

Here is a xml snippet:

<foal_line>
  <yob>0</yob> 
  <description>Tis The Alarm. Unplaced at 3 in NA. Dam of <B>SA MOKEN</B> (f, by Smoke Glacken. 2 wins at 2, $60,382 in NA. Won Ken Kendrick Memorial Futurity (SRP, $25,043). 2nd Kachina S. (RUI, $10,982). 3rd Ruidoso Thoroughbred Futurity (RUI, $7,787), etc.) Granddam of <B>Dream Kin</B> (f, by Desert God. 4 wins, 2 to 4, $127,880 in US. 2nd New Mexico Cup Juv. Fillies S.-R (ZIA, $33,440). 3rd C. O. "Ken" Kendrick Memorial S-R (SRP, $7,500), Lincoln H. [R] (RUI, $5,000), Carlos Salazar S. [N] (ALB, $4,000), etc.)</description> 
</foal_line>

My previous xslt snippet to create xhtml:

<tr>
   <td width="30px" style="vertical-align:text-top;">
  <xsl:value-of select="yob"/>
   </td>
   <td style="vertical-align:text-top;text-align:left;padding-left:2px">
  <xsl:value-of select="description" disable-output-escaping="yes" />
   </td>
</tr>

My current xsl-fo snippet:

<fo:table-row>
    <fo:table-cell>
       <fo:block><xsl:value-of select="yob"/></fo:block>
    </fo:table-cell>
    <fo:table-cell>
       <fo:block><xsl:value-of select="description"/></fo:block>
    </fo:table-cell>
</fo:table-row>

Edit: Here is what I'm really getting from the server. That's what I get for using IE to view my xml.

<foal_line>
  <yob>0</yob> 
  <description>Tis The Alarm. Unplaced at 3 in NA. Dam of &lt;B&gt;SA MOKEN&lt;/B&gt; (f, by Smoke Glacken. 2 wins at 2, $60,382 in NA. Won Ken Kendrick Memorial Futurity (SRP, $25,043). 2nd Kachina S. (RUI, $10,982). 3rd Ruidoso Thoroughbred Futurity (RUI, $7,787), etc.) Granddam of &lt;B&gt;Dream Kin&lt;/B&gt; (f, by Desert God. 4 wins, 2 to 4, $127,880 in US. 2nd New Mexico Cup Juv. Fillies S.-R (ZIA, $33,440). 3rd C. O. "Ken" Kendrick Memorial S-R (SRP, $7,500), Lincoln H. [R] (RUI, $5,000), Carlos Salazar S. [N] (ALB, $4,000), etc.)</description> 
</foal_line>

This is why suggested answer is not working.


Solution

  • Instead of doing xsl:value-of for <description>, do xsl:apply-templates. You can then create a template to match <B>. In the B template you can use fo:inline to make the text bold.

    Here's an example:

      <xsl:template match="foal_line">
        <fo:table-row>
          <fo:table-cell>
            <fo:block><xsl:value-of select="yob"/></fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block><xsl:apply-templates select="description"/></fo:block>
          </fo:table-cell>
        </fo:table-row>
      </xsl:template>
    
      <xsl:template match="description">
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="B">
        <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline>
      </xsl:template>
    

    Using your XML input and the above templates, the following output is generated:

    <fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
       <fo:table-cell>
          <fo:block>0</fo:block>
       </fo:table-cell>
       <fo:table-cell>
          <fo:block>Tis The Alarm. Unplaced at 3 in NA. Dam of <fo:inline font-weight="bold">SA MOKEN</fo:inline> (f, by Smoke Glacken. 2 wins at 2, $60,382 in NA. Won Ken Kendrick Memorial Futurity (SRP, $25,043). 2nd Kachina S. (RUI, $10,982). 3rd Ruidoso Thoroughbred Futurity (RUI, $7,787), etc.) Granddam of <fo:inline font-weight="bold">Dream Kin</fo:inline> (f, by Desert God. 4 wins, 2 to 4, $127,880 in US. 2nd New Mexico Cup Juv. Fillies S.-R (ZIA, $33,440). 3rd C. O. "Ken" Kendrick Memorial S-R (SRP, $7,500), Lincoln H. [R] (RUI, $5,000), Carlos Salazar S. [N] (ALB, $4,000), etc.)</fo:block>
       </fo:table-cell>
    </fo:table-row>
    

    Also, you should do the same for <yob> if it is also mixed content (text and other elements such as <B>).

    You can also do this for your XHTML XSLT so you don't have to use disable-output-escaping (which I try to avoid at all costs).