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

pre*_*eth 5 xslt xsl-fo xslt-2.0

我试图将我的数据和表格放入页面中心,如下表所示.但我只做了表格剩余的数据对齐和调整,我无法做到,因为我不熟悉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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

Ton*_*ham 13

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

text-align适用于fo:block,fo:external-graphic,fo:instream-foreign-object,和fo:table-and-caption.如果你需要设置一个内联FO,那么最简单的方法就是将它包装在一个fo:block具有的内联FO中text-align="center".

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

<fo:table-and-caption text-align="center">
     <fo:table>
Run Code Online (Sandbox Code Playgroud)

一个更长篇的水平居中任何块级对象的方法是将它放在一个fo:inline-container内部fo:block:

<fo:block text-align="center">
     <fo:inline-container>
         <fo:table>
Run Code Online (Sandbox Code Playgroud)

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

display-align适用于fo:region-body,fo:region-before,fo:region-after,fo:region-start,fo:region-end,fo:block-container,fo:external-graphic,fo:instream-foreign-object,fo:inline-container,和fo:table-cell.除此之外,这意味着您可以使垂直对齐成为页面主页设置的一部分,或者您可以fo:table将其置于全高中fo:block-container:

<fo:block-container display-align="center" height="100%">
    <fo:table-and-caption text-align="center">                
        <fo:table>
Run Code Online (Sandbox Code Playgroud)


Phi*_*yNJ 2

下面是一个示例 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>
Run Code Online (Sandbox Code Playgroud)