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

时间:2015-12-23 23:14:02

标签: xslt pdf-generation xslt-1.0

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

我想突出显示其节点<?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> 值为TOTAL为Gray(#CCCCCC)的行。我已经在XSLT中编写了该部分,但不知何故它无法正常工作。

XML:

<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如下:

<g id="top">
<path ....>
</g>
<g id="bottom">
<path ....>
</g>
<g id="boxes">
<rect ....>
</g>
<g id="txt">
<text transform="matrix(1 0 0 1 50 30)" class="svgtxt">TEXT</text>
</g>

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

1 个答案:

答案 0 :(得分:2)

您当前的XSLT有三个问题

  1. 您当前的表达式<xsl:when test="Mold=TOTAL">正在将元素Mold与元素TOTAL(不存在!)进行比较。您需要将其括在引号中以使其成为文字字符串,如下所示:<xsl:when test="Mold='TOTAL'">

  2. 您正在定义变量rowset,但它只会在xsl:when块的范围内,因此您无法在{{1}之后使用它}

  3. 引用属性时需要使用xsl:choose前缀。目前,您应$进行<fo:table-cell background-color="{rowcolor}"

  4. 设置颜色<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>