如何将文本和表格数据对齐到xsl中心

时间:2014-01-09 15:02:35

标签: xslt xslt-2.0 xsl-fo

我正在尝试将我的数据和表格放入页面中心,如下表所示。但我只做了表格中剩余的数据对齐和调整,我无法做到,因为我不熟悉xsl。

这是我的xsl代码:

<?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="records">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
   <fo:simple-page-master master-name="list">
       <fo:region-body></fo:region-body>
   </fo:simple-page-master>
 </fo:layout-master-set>
 <fo:page-sequence master-reference="list">
   <fo:flow flow-name="xsl-region-body">
       <fo:block text-align="right">
           <fo:table >
               <fo:table-body>
    <xsl:for-each select="./list">
                  <xsl:if test="position()&lt;= 1">
                        <fo:table-row>
                    <xsl:for-each select="./item">
                      <fo:table-cell text-align="center" 
           width="100px" border-color="black" border="2px">
                  <fo:block color="green" font-family="monospace" 
    font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt">
                                    <xsl:attribute name="color">
                               <xsl:choose>
                                  <xsl:when test="position() = 1 ">
                                     <xsl:text>black</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 2 ">
                                     <xsl:text>#333399</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 3 ">
                                     <xsl:text>#FF9900</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 4 ">
                                     <xsl:text>#96CCD8</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 5 ">
                                     <xsl:text>#19A347</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 6 ">
                                     <xsl:text>green</xsl:text>
                                  </xsl:when>
                                  <xsl:otherwise>
                                     <xsl:text>white</xsl:text>
                                  </xsl:otherwise>
                               </xsl:choose>
                            </xsl:attribute>
                            <xsl:value-of select="val"/>
                                    </fo:block>
                                </fo:table-cell>
                                </xsl:for-each>
                            </fo:table-row>
                            </xsl:if>
                        </xsl:for-each>
                     <xsl:for-each select="./list">
                     <xsl:if test="position()!=1">
                       <fo:table-row>
                       <xsl:for-each select="./item">
                      <fo:table-cell border="4px"  text-align="center">
                      <fo:block font-family="monospace" border-color="black"
 border-style="solid"  font-size="12pt" wrap-option="no-wrap"
 padding="5pt" space-before="5pt"  space-after="5pt">
                            <xsl:attribute name="background-color">
                               <xsl:choose>
                                  <xsl:when test="position() = 1 ">
                                     <xsl:text>#C1BFC4</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 2 ">
                                     <xsl:text>#B1A1C8</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 3 ">
                                     <xsl:text>#F9CAA0</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 4 ">
                                     <xsl:text>#96CCD8</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 5 ">
                                     <xsl:text>#C2D89A</xsl:text>
                                  </xsl:when>
                                  <xsl:when test="position() = 6 ">
                                     <xsl:text>green</xsl:text>
                                  </xsl:when>
                                  <xsl:otherwise>
                                     <xsl:text>red</xsl:text>
                                  </xsl:otherwise>
                               </xsl:choose>
                            </xsl:attribute>
                            <xsl:value-of select="val"/>
                         </fo:block>
                           </fo:table-cell>
                           </xsl:for-each>
                       </fo:table-row>
                       </xsl:if>
                   </xsl:for-each> 
               </fo:table-body>
           </fo:table>
       </fo:block>
    </fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>

这是我的xml代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<records>
<list>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>Subject Number</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>Monthly Dairy Contact</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>3-Month Safety Contact</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>Annual visit</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>Suspected HZ follow-up Visit</val>
    </item>
</list>
<list>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>49210</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>x</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>15-sep-2012</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val></val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val></val>
    </item>
</list>
<list>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>49210</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>x</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>15-sep-2012</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>test</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val></val>
    </item>
</list>
<list>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>49210</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>x</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>15-sep-2012</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>test</val>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
        <val>test</val>
    </item>
</list>
</records>

注意:xml只保存放在表格中的数据以pdf格式显示,但其余的内容可以静态添加到xsl文件中,但我不知道如何正确对齐,就像我在图像中所示。 任何人都可以根据重新采购在我的代码中进行修改。 谢谢

2 个答案:

答案 0 :(得分:11)

对于水平居中,简短的回答是使用text-align="center"。 (见https://www.w3.org/TR/xsl11/#text-align

text-align适用于fo:blockfo:external-graphicfo:instream-foreign-objectfo:table-and-caption。如果您需要将内联FO置于中心位置,最简单的方法是将其包含在fo:block的{​​{1}}中。

text-align="center"水平居中的一种方法是将其放入fo:table

fo:table-and-caption

将任何块级对象水平居中的更长篇的方法是将其放在<fo:table-and-caption text-align="center"> <fo:table> 内的fo:inline-container内:

fo:block

对于垂直居中,请使用<fo:block text-align="center"> <fo:inline-container> <fo:table> 。 (见https://www.w3.org/TR/xsl11/#display-align

display-align="center"适用于display-alignfo:region-bodyfo:region-beforefo:region-afterfo:region-startfo:region-end,{{1} },fo:block-containerfo:external-graphicfo:instream-foreign-object。除此之外,这意味着您可以使垂直对齐成为页面母版设置的一部分,或者您可以将fo:inline-container放在一个全高fo:table-cell的中心位置:

fo:table

答案 1 :(得分:2)

下面是一个示例Of,其中显示了如何设置边距并使表格居中:

<?xml version="1.0" encoding="iso-8859-1"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="list">
            <fo:region-body margin-left=".5in" margin-right=".5in" margin-top=".5in" margin-bottom=".5in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="list">
        <fo:flow flow-name="xsl-region-body">
            <fo:block text-align="center">
                <fo:table text-align="center" start-indent=".125in">
                    <fo:table-body>
                        <fo:table-row>
                            <fo:table-cell text-align="center" width="100px" border-color="black" border="2px">
                                <fo:block font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt" color="black">Subject Number</fo:block>
                            </fo:table-cell>
                            <fo:table-cell text-align="center" width="100px" border-color="black" border="2px">
                                <fo:block font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt" color="#333399">Monthly Dairy Contact</fo:block>
                            </fo:table-cell>
                            <fo:table-cell text-align="center" width="100px" border-color="black" border="2px">
                                <fo:block font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt" color="#FF9900">3-Month Safety Contact</fo:block>
                            </fo:table-cell>
                            <fo:table-cell text-align="center" width="100px" border-color="black" border="2px">
                                <fo:block font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt" color="#96CCD8">Annual visit</fo:block>
                            </fo:table-cell>
                            <fo:table-cell text-align="center" width="100px" border-color="black" border="2px">
                                <fo:block font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt" color="#19A347">Suspected HZ follow-up Visit</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C1BFC4">49210</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#B1A1C8">x</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#F9CAA0">15-sep-2012</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#96CCD8"></fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C2D89A"></fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C1BFC4">49210</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#B1A1C8">x</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#F9CAA0">15-sep-2012</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#96CCD8">test</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C2D89A"></fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C1BFC4">49210</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#B1A1C8">x</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#F9CAA0">15-sep-2012</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#96CCD8">test</fo:block>
                            </fo:table-cell>
                            <fo:table-cell border="4px" text-align="center">
                                <fo:block font-family="monospace" border-color="black" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" background-color="#C2D89A">test</fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>