how to align text and table data into center in xsl

preeth picture preeth · Jan 9, 2014 · Viewed 33.7k times · Source

i am trying to put my data and table into the center of page as shown in below table. but i have done only the table stuff remaining data alignments and adjustments i am not able to do, as i am not familiar with xsl.

and here is my xsl code :

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

and here is my xml code :

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

NOTE: the xml holds only the data which is to be placed in the table comes in pdf but the rest of the matter can statically added in the xsl file but i dont know how to align properly as i shown in the image. can any one do the modifications in my code as per the reqiurement. thank you

Answer

Tony Graham picture Tony Graham · Jun 21, 2016

For horizontal centering, the short answer is to use text-align="center". (See https://www.w3.org/TR/xsl11/#text-align)

text-align applies to fo:block, fo:external-graphic, fo:instream-foreign-object, and fo:table-and-caption. If there's an inline FO that you need to center, the simplest thing to do would be to wrap it in an fo:block that has the text-align="center".

One way to horizontally center the fo:table is to put it in an fo:table-and-caption:

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

A more long-winded way to horizontally center any block-level object is to put it inside an fo:inline-container inside the fo:block:

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

For vertical centering, use display-align="center". (See https://www.w3.org/TR/xsl11/#display-align)

display-align applies to 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, and fo:table-cell. Among other things, this means that you can make the vertical alignment be part of the page master set-up or that you can center just the fo:table by putting it in a full-height fo:block-container:

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