使用XSLT创建PDF时为特定行着色



我目前正在用XSLT制作PDF。我能够用正确的数据和标题正确地生成PDF。

我想将节点<Mold>值为TOTAL的行高亮显示为灰色(#CCCCCC)。我已经用XSLT编写了该部分,但不知何故它不起作用。

XML:

<?xml version="1.0" encoding="utf-8"?>
<Rowsets DateCreated="2015-12-23T14:57:50" EndDate="2015-10-26T17:21:17" StartDate="2015-10-26T17:21:17" Version="15.0 SP4 Patch 4 (Jun 3, 2015)">
    <Rowset>
        <Columns>
            <Column Description="" MaxRange="1" MinRange="0" Name="Material" SQLDataType="1" SourceColumn="Material"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="ShiftTime" SQLDataType="1" SourceColumn="ShiftTime"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Total_Scrap" SQLDataType="8" SourceColumn="Total_Scrap"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Total_Confirmed" SQLDataType="8" SourceColumn="Total_Confirmed"/>
        </Columns>
        <Row>
            <Material>300</Material>
            <ShiftTime>18:00</ShiftTime>
            <Mold>111</Mold>
            <Press>25</Press>
            <Total_Scrap>20.0</Total_Scrap>
            <Total_Confirmed>200.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300</Material>
            <ShiftTime>18:00</ShiftTime>
            <Mold>Summary</Mold>
            <Press> --- </Press>
            <Total_Scrap>20.0</Total_Scrap>
            <Total_Confirmed>200.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300</Material>
            <ShiftTime>Total Shift</ShiftTime>
            <Mold>TOTAL</Mold>
            <Press> --- </Press>
            <Total_Scrap>20.0</Total_Scrap>
            <Total_Confirmed>200.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300-1</Material>
            <ShiftTime>18:00</ShiftTime>
            <Mold>222</Mold>
            <Press>16</Press>
            <Total_Scrap>8.0</Total_Scrap>
            <Total_Confirmed>111.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300-1</Material>
            <ShiftTime>18:00</ShiftTime>
            <Mold>333</Mold>
            <Press>23</Press>
            <Total_Scrap>10.0</Total_Scrap>
            <Total_Confirmed>199.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300-1</Material>
            <ShiftTime>18:00</ShiftTime>
            <Mold>Summary</Mold>
            <Press> --- </Press>
            <Total_Scrap>18.0</Total_Scrap>
            <Total_Confirmed>310.0</Total_Confirmed>
        </Row>
        <Row>
            <Material>300-1</Material>
            <ShiftTime>Total Shift</ShiftTime>
            <Mold>TOTAL</Mold>
            <Press> --- </Press>
            <Total_Scrap>18.0</Total_Scrap>
            <Total_Confirmed>310.0</Total_Confirmed>
        </Row>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="Break" MaxRange="100" MinRange="0" Name="Name" SQLDataType="1" SourceColumn="Name"/>
        </Columns>
    </Rowset>
    <Rowset>
        <Columns>
            <Column Description="" MaxRange="1" MinRange="0" Name="Mold" SQLDataType="1" SourceColumn="Mold"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Press" SQLDataType="1" SourceColumn="Press"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Total_Scrap" SQLDataType="8" SourceColumn="Total_Scrap"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Total_Confirmed" SQLDataType="8" SourceColumn="Total_Confirmed"/>
        </Columns>
        <Row>
            <Mold>222</Mold>
            <Press>16</Press>
            <Total_Scrap>20</Total_Scrap>
            <Total_Confirmed>100</Total_Confirmed>
        </Row>
        <Row>
            <Mold>333</Mold>
            <Press>23</Press>
            <Total_Scrap>30</Total_Scrap>
            <Total_Confirmed>200</Total_Confirmed>
        </Row>
        <Row>
            <Mold>111</Mold>
            <Press>25</Press>
            <Total_Scrap>30</Total_Scrap>
            <Total_Confirmed>300</Total_Confirmed>
        </Row>
    </Rowset>
</Rowsets>

我试图获得上述功能的XSLT的一部分:

<xsl:for-each select="Row">
                                                        <fo:table-row height="13pt">
                                                            <xsl:choose>
                                                                <xsl:when test="Mold=Total">
                                                                    <xsl:variable name="rowcolor">#CCCCCC</xsl:variable>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                    <xsl:variable name="rowcolor">#FFFFFF</xsl:variable>
                                                                </xsl:otherwise>
                                                            </xsl:choose>
                                                            <xsl:for-each select="child::*">
                                                                <fo:table-cell background-color="{rowcolor}" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
                                                                    <fo:block font-weight="normal" text-align="left">
                                                                        <xsl:value-of select="."/>
                                                                    </fo:block>
                                                                </fo:table-cell>
                                                            </xsl:for-each>
                                                        </fo:table-row>
                                                    </xsl:for-each>

整个XSLT如下:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Helvetica" font-size="10px">
            <fo:layout-master-set>
                <fo:simple-page-master margin-bottom="0.5cm" margin-left="0.75cm" margin-right="0.75cm" margin-top="0.5cm" master-name="first" page-height="11in" page-width="17in">
                    <fo:region-body margin-bottom="0.5cm" margin-top="0.25cm"/>
                    <fo:region-before extent="0cm"/>
                    <fo:region-after extent="0.5cm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <xsl:for-each select="Rowsets">
                <fo:page-sequence master-reference="first">
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="8pt" line-height="6pt" text-align-last="justify">
                            Shift Report
                            <fo:inline id="Date">
                                Date [currentDate]
                            </fo:inline>
                            <fo:leader leader-pattern="space"/>
                            Page
                            <fo:page-number/>
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:for-each select="Rowset">
                            <xsl:choose>
                                <xsl:when test="position()=1">
                                    <fo:table border-color="black" border-style="solid" border-width="1pt" table-layout="fixed" width="100%">
                                        <xsl:variable name="columns">
                                            <xsl:value-of select="count(Columns/Column)"/>
                                        </xsl:variable>
                                        <xsl:for-each select="Columns/Column">
                                            <xsl:choose>
                                                <xsl:when test="position()&lt;3">
                                                    <fo:table-column column-width="70pt"/>
                                                </xsl:when>
                                                <xsl:when test="position()&gt;2 and position()&lt;5">
                                                    <fo:table-column column-width="60pt"/>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                    <fo:table-column/>
                                                </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:for-each>
                                        <fo:table-body font-size="11pt">
                                            <fo:table-row height="14pt">
                                                <fo:table-cell background-color="#000000" border-style="solid" border-width="1pt" number-columns-spanned="{$columns}" padding-left="5pt" padding-top="5pt">
                                                    <fo:block color="#FFFFFF" font-weight="bold" text-align="center">
                                                        Shift Report
                                                    </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                            <fo:table-row height="14pt">
                                                <xsl:for-each select="Columns/Column">
                                                    <fo:table-cell background-color="#CCCCCC" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
                                                        <fo:block color="#000000" font-weight="bold" text-align="left">
                                                            <xsl:choose>
                                                                <xsl:when test="contains(@Name, 'Percent')">
                                                                    <xsl:value-of select="substring-before(@Name, 'Percent')"/>
                                                                    <xsl:text> %</xsl:text>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                    <xsl:value-of select="translate(@Name,'_',' ')"/>
                                                                </xsl:otherwise>
                                                            </xsl:choose>
                                                        </fo:block>
                                                    </fo:table-cell>
                                                </xsl:for-each>
                                            </fo:table-row>
                                            <xsl:for-each select="Row">
                                                <fo:table-row height="14pt">
                                                    *<xsl:choose>
                                                        <xsl:when test="Mold=TOTAL">
                                                            <xsl:variable name="rowcolor">#CCCCCC</xsl:variable>
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                            <xsl:variable name="rowcolor">#FFFFFF</xsl:variable>
                                                        </xsl:otherwise>
                                                    </xsl:choose>
                                                    <xsl:for-each select="child::*">
                                                        <fo:table-cell background-color="{rowcolor}" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
                                                            <fo:block font-weight="normal" text-align="left">
                                                                <xsl:value-of select="."/>
                                                            </fo:block>*
                                                        </fo:table-cell>
                                                    </xsl:for-each>
                                                </fo:table-row>
                                            </xsl:for-each>
                                        </fo:table-body>
                                    </fo:table>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:choose>
                                        <xsl:when test="Columns/Column[1]/@Description = 'Break'">
                                            <fo:block page-break-before="always"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <fo:table border-color="black" border-style="solid" border-width="1pt" table-layout="fixed" width="100%">
                                                <xsl:variable name="columns">
                                                <xsl:value-of select="count(Columns/Column)"/>
                                            </xsl:variable>
                                                <xsl:for-each select="Columns/Column">
                                                    <fo:table-column/>
                                                </xsl:for-each>
                                                <fo:table-body font-size="10pt">
                                                    <fo:table-row height="13pt">
                                                        <fo:table-cell background-color="#000000" border-style="solid" border-width="1pt" number-columns-spanned="{$columns}" padding-left="5pt" padding-top="5pt">
                                                            <fo:block color="#FFFFFF" font-weight="bold" text-align="center">
                                                            Shift Report
                                                        </fo:block>
                                                        </fo:table-cell>
                                                    </fo:table-row>
                                                    <fo:table-row height="13pt">
                                                        <xsl:for-each select="Columns/Column">
                                                            <fo:table-cell background-color="#CCCCCC" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
                                                                <fo:block color="#000000" font-weight="bold" text-align="left">
                                                                    <xsl:choose>
                                                                <xsl:when test="contains(@Name, 'Percent')">
                                                                    <xsl:value-of select="substring-before(@Name, 'Percent')"/>
                                                                    <xsl:text> %</xsl:text>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                    <xsl:value-of select="translate(@Name,'_',' ')"/>
                                                                </xsl:otherwise>
                                                            </xsl:choose>
                                                                </fo:block>
                                                            </fo:table-cell>
                                                        </xsl:for-each>
                                                    </fo:table-row>
                                                    <xsl:for-each select="Row">
                                                        <fo:table-row height="13pt">
                                                            <xsl:choose>
                                                                <xsl:when test="Mold=Total">
                                                                    <xsl:variable name="rowcolor">#CCCCCC</xsl:variable>
                                                                </xsl:when>
                                                                <xsl:otherwise>
                                                                    <xsl:variable name="rowcolor">#FFFFFF</xsl:variable>
                                                                </xsl:otherwise>
                                                            </xsl:choose>
                                                            <xsl:for-each select="child::*">
                                                                <fo:table-cell background-color="{rowcolor}" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
                                                                    <fo:block font-weight="normal" text-align="left">
                                                                        <xsl:value-of select="."/>
                                                                    </fo:block>
                                                                </fo:table-cell>
                                                            </xsl:for-each>
                                                        </fo:table-row>
                                                    </xsl:for-each>
                                                </fo:table-body>
                                            </fo:table>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:for-each>
                    </fo:flow>
                </fo:page-sequence>
            </xsl:for-each>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

我做错了什么?有人能帮忙吗?

当前的XSLT 有三个问题

  1. 当前表达式<xsl:when test="Mold=TOTAL">正在比较元素Mold和元素TOTAL(该元素不存在!)。您需要用引号将其括起来,使其成为一个文字字符串,如下所示:<xsl:when test="Mold='TOTAL'">

  2. 您正在定义一个变量rowset,但它只在xsl:when块的范围内,因此在xsl:choose 之后将无法使用它

  3. 引用属性时需要使用$前缀。目前您使用<fo:table-cell background-color="{rowcolor}"来设置颜色,而您应该使用<fo:table-cell background-color="{$rowcolor}"

试试这个代码块而不是

<xsl:for-each select="Row">
<fo:table-row height="14pt">
    <xsl:variable name="rowcolor">
        <xsl:choose>
            <xsl:when test="Mold='TOTAL'">#CCCCCC </xsl:when>
            <xsl:otherwise>#FFFFFF</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:for-each select="child::*">
        <fo:table-cell background-color="{$rowcolor}" border-style="solid" border-width="1pt" padding-left="5pt" padding-top="5pt">
            <fo:block font-weight="normal" text-align="left">
                <xsl:value-of select="."/>
            </fo:block>
        </fo:table-cell>
    </xsl:for-each>
</fo:table-row>
</xsl:for-each>

相关内容

  • 没有找到相关文章

最新更新