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



我正在尝试将我的数据和表格放在页面中心,如下表所示。 但是我只做了表格的东西,剩下的数据对齐和调整我无法做到,因为我不熟悉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仅包含要放置在表中的数据,但其余部分可以静态添加到XSL文件中,但我不知道如何正确对齐,如图所示。任何人都可以根据要求对我的代码进行修改吗?谢谢

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

text-align适用于fo:blockfo:external-graphicfo:instream-foreign-objectfo:table-and-caption。 如果您需要居中内联 FO,最简单的方法是将其包装在具有text-align="center"fo:block中。

fo:table水平居中的一种方法是将其放在fo:table-and-caption中:

<fo:table-and-caption text-align="center">
     <fo:table>
水平

居中任何块级对象的更冗长的方法是将其放在fo:block内的fo:inline-container中:

<fo:block text-align="center">
     <fo:inline-container>
         <fo:table>

对于垂直居中,请使用 display-align="center" 。(见 https://www.w3.org/TR/xsl11/#display-align)

display-align适用于fo:region-bodyfo:region-beforefo:region-afterfo:region-startfo:region-endfo:block-containerfo:external-graphicfo:instream-foreign-objectfo:inline-containerfo:table-cell。 除此之外,这意味着您可以使垂直对齐成为页面母版设置的一部分,或者您可以通过将其放在全高fo:block-container中来仅居中fo:table

<fo:block-container display-align="center" height="100%">
    <fo:table-and-caption text-align="center">                
        <fo:table>
下面是

一个示例Fo,它向您展示了如何设置边距并使表格居中:

<?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>

最新更新