我们如何使用XSLT样式表

时间:2015-06-28 15:53:37

标签: xslt html-table stylesheet apache-fop

我正在使用Apache FOP将动态生成的HTML转换为PDF。一切正常,但当表跨越多个页面时,我无法重复表头。

标本HTML代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>AAA</title>  
</head>
<body> 
<table border="1"> 
<tr><th>Sample header</th></tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
<tr>
    <td height="500">HELLO - THIS LOOKS GOOD</td>
</tr>
</table>
</body>
</html>

XLS:

<xsl:template match="table">
  <xsl:apply-templates select="caption"/>
  <fo:table table-layout="fixed" width="100%"><xsl:call-template name="common-atts"/>
    <xsl:apply-templates select="colgroup|col"/>
    <xsl:variable name="tr1" select="(tr|thead/tr|tbody/tr|tfoot/tr)[1]"/>
    <xsl:variable name="cols" select="colgroup/col|col"/>
    <xsl:call-template name="mock-col">
      <xsl:with-param name="cols" select="(count($tr1/*[not(@colspan)])+sum($tr1/*/@colspan))
        -(count($cols[not(@colspan)])+sum($cols/@colspan))"/>
    </xsl:call-template>
    <xsl:apply-templates select="thead|tfoot|tbody"/>
    <xsl:if test="tr">
      <fo:table-body><xsl:call-template name="common-atts"/>
        <xsl:apply-templates select="tr"/>
      </fo:table-body>
    </xsl:if>
  </fo:table>  
</xsl:template>
<xsl:template match="xhtml:th|th">
  <fo:table-cell font-weight="bold" padding=".1em"><xsl:call-template name="common-atts"/>
    <xsl:if test="ancestor::table/@cellpadding = 0">
        <xsl:attribute name="padding">0mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@cellpadding = 1">
        <xsl:attribute name="padding">0.3mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@cellpadding = 2">
        <xsl:attribute name="padding">0.6mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@cellpadding = 3">
        <xsl:attribute name="padding">0.9mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@cellpadding = 4">
        <xsl:attribute name="padding">1.2mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@cellpadding > 4">
        <xsl:attribute name="padding">1.5mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@border = 0">
        <xsl:attribute name="border-style">none</xsl:attribute>
        <xsl:attribute name="border-width">0.0mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@border = 1">
        <xsl:attribute name="border-style">solid</xsl:attribute>
        <xsl:attribute name="border-width">0.2mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@border = 2">
        <xsl:attribute name="border-style">solid</xsl:attribute>
        <xsl:attribute name="border-width">0.3mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@border = 3">
        <xsl:attribute name="border-style">solid</xsl:attribute>
        <xsl:attribute name="border-width">0.4mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="ancestor::table/@border > 3">
        <xsl:attribute name="border-style">solid</xsl:attribute>
        <xsl:attribute name="border-width">0.5mm</xsl:attribute>
    </xsl:if>
    <xsl:if test="@colspan">
      <xsl:attribute name="number-columns-spanned"><xsl:value-of select="@colspan"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@rowspan">
      <xsl:attribute name="number-rows-spanned"><xsl:value-of select="@rowspan"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@bgcolor">
        <xsl:attribute name="background-color"><xsl:value-of select="@bgcolor"/></xsl:attribute>
    </xsl:if>
    <xsl:if test="@color">
        <xsl:attribute name="color"><xsl:value-of select="@color"/></xsl:attribute>
    </xsl:if>
    <fo:block>
      <xsl:if test="parent::xhtml:tr/parent::xhtml:thead|parent::tr/parent::thead">
        <xsl:attribute name="text-align">center</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </fo:block>
  </fo:table-cell>
</xsl:template>

0 个答案:

没有答案