7

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

preeth
  • 365
  • 3
  • 7
  • 20
  • Say clearly what modifications you were not able to code. – Mathias Müller Jan 09 '14 at 15:07
  • as i was shown in the image... i am not yet added any text, if you can add some sample text then it would be great. – preeth Jan 09 '14 at 15:15
  • IMO, When dealing with how a FO file renders a PDF, it is best to first look at the FO file. This will determine why your output is not rendering as you expect. From there you can make adjustments to your xslt. You should first show the FO file (at least the relevant part of your FO) and where/what you need help with. – PhillyNJ Jan 09 '14 at 17:40
  • you mean my xml data(which holds the data to be outputted into the final pdf).... just now i have added that in my question but the xml hold only data which is to be reside in the table the rest of the matter should be statically put in the xsl file. – preeth Jan 09 '14 at 17:50
  • but some how i have done the complete part but i am getting problem when theres no data comes into any table cell in the table then the table cells gets overlapped,,, could you please solve that... you can do it at http://stackoverflow.com/questions/21025444/how-to-adjust-table-cells-properly-in-xsl – preeth Jan 09 '14 at 17:52

2 Answers2

17

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>
Tony Graham
  • 7,306
  • 13
  • 20
2

Below is an example Fo, which shows you how to set the margins and center your table:

<?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>
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64
  • but table cells getting overlapped when there is no values in the cell. can you please rectify that. – preeth Jan 10 '14 at 04:26
  • I have provided an example that I believe covers your question. Have you at least tried to fix the overlapping? The SO is here to help you solve your coding problems, not do all the work for you :) However, I will give you a hint - try putting spaces in the empty fo:blocks using an entity - http://stackoverflow.com/questions/1461649/how-to-insert-nbsp-in-xslt – PhillyNJ Jan 10 '14 at 12:18