如何使用xsl-fo </b>翻译数据中的<b>标签

时间:2011-11-16 18:16:21

标签: xslt xsl-fo

我有报道说我必须使用来自MySQL数据库的xml数据的xsl-fo转换为PDF。

我正在使用的xml结构已用于创建HTML报告。

我的xsl:value-of语句中添加了disable-output-escaping =“yes”,某些字段已经包含了我可以在HTML报告中使用的html标记。

如何在xsl-fo中执行类似的操作?有没有办法可以将标签更改为fo:inline?或者也许我可以在数据库输出中更改哪些是等效的PDF版本的粗体?

这是一个xml片段:

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

我之前创建xhtml的xslt代码段:

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

我当前的xsl-fo片段:

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

修改 这是我真正从服务器获得的。这就是我用IE浏览我的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>

这就是为什么建议的答案无效。

2 个答案:

答案 0 :(得分:2)

而不是为xsl:value-of执行<description>,而是xsl:apply-templates。然后,您可以创建一个匹配<B>的模板。在B模板中,您可以使用fo:inline将文字设为粗体。

以下是一个例子:

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

使用XML输入和上述模板,将生成以下输出:

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

此外,如果<yob>也是混合内容(文本和其他元素,例如<B>),则应对disable-output-escaping执行相同操作。

您也可以为XHTML XSLT执行此操作,这样您就不必使用{{1}}(我会不惜一切代价避免使用它)。

答案 1 :(得分:1)

我知道这个问题相当陈旧。我正在使用Apache FOP并且遇到了编码或原始HTML处理的这种行为。这是我的解决方案,我已经工作了几个小时。

XML:

    <foal_line>
        <yob>0</yob> 
        <description><html xmlns="http://www.w3.org/1999/xhtml">HTML text can be <b>bold</b> and <i>italic</i></html></description>
    </foal_line>

XSL:

<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 >

    <!--  Output in XML -->
    <xsl:output method="xml" version="1.0" indent="yes" />

    <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 
    <!-- Templates                                                          -->
    <!-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 
    <xsl:template match="/">    
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="seite" page-width="210mm" page-height="297mm" margin="20mm 20mm 20mm 20mm">
                    <fo:region-body margin="20mm 0mm 10mm 0mm"/>
                    <fo:region-before region-name="header"/>
                    <fo:region-after region-name="footer"/>
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence id="seite" master-reference="seite">
                <fo:static-content flow-name="footer">
                    <fo:block>Footer</fo:block>
                </fo:static-content>
                <fo:static-content flow-name="header">
                    <fo:block>Header</fo:block>
                </fo:static-content>
                <fo:flow flow-name="xsl-region-body">
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>         
        </fo:root>
    </xsl:template>

    <xsl:template match="foal_line">
        <fo:block-container>
            <fo:block>Yob: <xsl:value-of select="yob"/></fo:block>
            <fo:block>Desc: 
                <xsl:apply-templates select="description/xhtml:html"/>
            </fo:block>         
        </fo:block-container>
    </xsl:template>

    <xsl:template match="description">
        <xsl:message>Description</xsl:message>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="xhtml:b">
        <xsl:message>Bold</xsl:message>
        <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline>
    </xsl:template>

    <xsl:template match="xhtml:i">
        <xsl:message>italic</xsl:message>
        <fo:inline font-style="italic"><xsl:apply-templates/></fo:inline>
    </xsl:template>
</xsl:stylesheet>

最后,我得到了一个结构良好的FO,可以将其翻译成PDF格式。