XSL-FO文档:在具体的fo:table-row中插入break-before =“ page”属性

时间:2018-11-10 18:25:18

标签: c# xml visual-studio-2008 .net-3.5 xsl-fo

我有一个XSL-FO文档。该文档有很多fo:table嵌套。 有一个内部fo:table,它具有9列和很多行,在此示例中,第一行代表表头,其他行,内容从1到20。在下面的示例中,您可以看到,作为注释,对于每个FOPTable(fo:table),它都表示一个唯一的标识符,但是对于同一张表,此标识符并不总是相同的。它是由创建此XSL-FO文档的应用程序随机生成的。

我的问题是:我想找到其第一列标题值设置为HEADER_COL1的表(此值始终相同),在此示例中,标识为id:997的表(无法搜索的表的内容)根据此标识符,因为每次应用程序创建XSL-FO文档时它都会随机更改)。找到所需的表格后,请在下面进行操作:

  1. 如何为具体插入属性break-before =“ page” fo:表行项目?
  2. 我想获取此内部表的行数。

例如:

例如,如果要为第15个fo:table-row项插入此属性,该怎么做?我想要一个经过参数化的通用方法,该方法以一个数字作为参数,该数字指示fo:table-row将此属性放在何处。

我知道我可以使用以下内容阅读XSL-FO文档:

string xmlFile = File.ReadAllText(@"C:\Temp\MyXSLFO.xml");
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlFile);

但是一旦加载,我就不知道该怎么做。

注意:我正在使用Visual Studio 2008,C#和.NET 3.5。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:page-sequence force-page-count="no-force" master-reference="first" initial-page-number="1">
<fo:flow flow-name="xsl-region-body">
<!--  begin table FOPTable { id: 987 cur_size: 28.7 prv_size: 20.0 prev: <none> cols: 2 locks: 2 flow:FOPFlow: { type: BodyFlow size:4 region:FOPRegion: { type:3 extent:0.0}} } 
  --> 
<fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
</fo:table>
<!--  endof table id FOPTable { id: 987 cur_size: 28.7 prv_size: 20.0 prev: <none> cols: 2 locks: 5 flow:FOPFlow: { type: BodyFlow size:4 region:FOPRegion: { type:3 extent:0.0}} } 
  --> 
<!--  begin table FOPTable { id: 992 cur_size: 28.7 prv_size: 20.0 prev: <none> cols: 2 locks: 2 flow:FOPFlow: { type: BodyFlow size:3 region:FOPRegion: { type:3 extent:0.0}} } 
  --> 
<fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
</fo:table>
<!--  endof table id FOPTable { id: 992 cur_size: 28.7 prv_size: 20.0 prev: <none> cols: 2 locks: 4 flow:FOPFlow: { type: BodyFlow size:3 region:FOPRegion: { type:3 extent:0.0}} } 
  --> 
<!--  begin table FOPTable { id: 995 cur_size: 28.7 prv_size: 0.0 prev: <none> cols: 2 locks: 2 flow:FOPFlow: { type: BodyFlow size:2 region:FOPRegion: { type:3 extent:0.0}} } 
  --> 
<fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">

<!--  begin table FOPTable { id: 996 cur_size: 0.0 prv_size: 20.41 prev: 995 cols: 1 locks: 2 flow:null } 
  --> 
<fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">

<!--  begin table FOPTable { id: 997 cur_size: 0.0 prv_size: 20.409999999999997 prev: 996 cols: 9 locks: 5 flow:null } 
  --> 
<fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="4.709999999999996cm" /> 
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="3.9cm" /> 
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="2.87cm" /> 
  <fo:table-column column-width="2.87cm" /> 
<fo:table-header>
<fo:table-row>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL1</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL2</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL3</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL4</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL5</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL6</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL7</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL8</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
  <fo:block>HEADER_COL9</fo:block> 
  </fo:table-cell>
  </fo:table-row>
  </fo:table-header>
<fo:table-body>
<fo:table-row>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL1_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL2_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL3_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL4_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL5_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL6_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-right="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block text-align="end">ROW1_COL7_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL8_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW1_COL9_VALUE</fo:block> 
  </fo:table-cell>
  </fo:table-row>


<!-- A LOT OF ROWS HERE -->

<!-- I want to insert a break-before attribute as below -->
<fo:table-row break-before="page">
<!-- columns here -->
</fo:table-row>

<!-- MORE ROWS HERE -->

<!-- LAST ROW NEXT -->
<fo:table-row>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL1_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL2_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL3_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL4_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL5_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL6_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-right="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block text-align="end">ROW20_COL7_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL8_VALUE</fo:block> 
  </fo:table-cell>
<fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" font-family="sans-serif" font-size="7pt">
  <fo:block>ROW20_COL9_VALUE</fo:block> 
  </fo:table-cell>
  </fo:table-row>
  </fo:table-body>
  </fo:table>
<!--  endof table id FOPTable { id: 997 cur_size: 28.7 prv_size: 20.409999999999997 prev: 996 cols: 9 locks: 6 flow:null } 
  --> 
</fo:table>

</fo:table>

</fo:flow>
</fo:page-sequence>
</fo:root>

1 个答案:

答案 0 :(得分:0)

获取数据并删除所有单元格和其他不重要的内容,只需将身份XSL应用于XSL FO并仅修改目标行即可。

以该XML开头:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <fo:page-sequence force-page-count="no-force" master-reference="first" initial-page-number="1">
        <fo:flow flow-name="xsl-region-body">
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
            </fo:table>
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
            </fo:table>
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
                <fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">
                    <fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="4.709999999999996cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="3.9cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-column column-width="2.87cm" /> 
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL1</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL2</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL3</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL4</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL5</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL6</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL7</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL8</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL9</fo:block> 
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-header>
                        <fo:table-body>
                            <fo:table-row/>
                            <fo:table-row/>
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                        </fo:table-body>
                    </fo:table>
                </fo:table>

            </fo:table>

        </fo:flow>
    </fo:page-sequence>
</fo:root>

并应用此XSL,它将按原样输出所有内容,除了在目标表的第12行插入一个分页符的模板之外:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    version="2.0">
    <xsl:param name="breakrow" select="12"/>

    <xsl:template match="fo:table-row">
        <xsl:variable name="rowcnt" select="count(preceding-sibling::fo:table-row)"/>
        <xsl:choose>
            <xsl:when test="parent::fo:table-header">
                <fo:table-row>
                    <xsl:apply-templates/>
                </fo:table-row>
            </xsl:when>
            <xsl:when test="$rowcnt = $breakrow and ancestor::fo:table[1]/fo:table-header/fo:table-row[1]/fo:table-cell[1]/fo:block/text() = 'HEADER_COL1'">
                <fo:table-row break-before="page"/>
            </xsl:when>
            <xsl:otherwise>
                <fo:table-row/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

结果是这样的:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <fo:page-sequence force-page-count="no-force" master-reference="first" initial-page-number="1">
        <fo:flow flow-name="xsl-region-body">
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
            </fo:table>
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
            </fo:table>
            <fo:table font-size="8pt" font-family="sans-serif" space-before.optimum="0.05cm" table-layout="fixed">
                <fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">
                    <fo:table font-size="8pt" font-family="sans-serif" table-layout="fixed">
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="4.709999999999996cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="3.9cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-column column-width="2.87cm"/> 
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL1</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL2</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL3</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL4</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL5</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL6</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL7</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL8</fo:block> 
                                </fo:table-cell>
                                <fo:table-cell padding-top="2.0pt" padding-left="2.0pt" border-style="solid" border-width="0.5pt" border-color="#000000" background-color="#D6DEE7" font-family="sans-serif">
                                    <fo:block>HEADER_COL9</fo:block> 
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-header>
                        <fo:table-body>
                            <fo:table-row/>
                            <fo:table-row/>
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row break-before="page"/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                            <fo:table-row/>                            
                        </fo:table-body>
                    </fo:table>
                </fo:table>

            </fo:table>

        </fo:flow>
    </fo:page-sequence>
</fo:root>

我传入了12作为参数,并假设您只有一个地方,即第12行。您当然可以使用“ mod”并每12行执行一次,或执行您想要的任何操作。

例如:

 <xsl:when test="$rowcnt mod $breakrow = $breakrow - 1 and ancestor::fo:table[1]/fo:table-header/fo:table-row[1]/fo:table-cell[1]/fo:block/text() = 'HEADER_COL1'">

当然,您可以将其添加到行中以获取所有单元格内容的输出。