Link to home
Start Free TrialLog in
Avatar of ank5
ank5Flag for India

asked on

Formatting issues in XSL FO

Below is my XML

<?xml version='1.0' encoding='UTF-8'?>
<result>
  <settings>  
    <variable name="$user_name$">S1</variable>
    <reportname>Client Info</reportname>
  </settings>
  <objects name="Client Capability">
    <object>
      <group_name type="string">c1</group_name>
    </object>
    <object>
      <group_name type="string">c2</group_name>
    </object>
    <object>
      <group_name type="string">c3</group_name>
    </object>
  </objects>
  <objects name="Execution Details">
    <object>
      <user_name type="string">user1</user_name>
      <exec_date type="time">2016/12/12 09:20:02</exec_date>
    </object>
  </objects>
  <objects name="Count">
    <object>
      <cnt type="double">3</cnt>
    </object>
  </objects>
</result>

Open in new window


and the XSL FO that I have written to convert it to PDF

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="fo java" version="1.0">
   

   
   <xsl:variable name="m_page_height">21</xsl:variable>
   <xsl:variable name="m_page_width">29.7</xsl:variable>
   
    <xsl:attribute-set name="m_table-header">
        <xsl:attribute name="font-size">7pt</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set name="m_table-cell">
        <xsl:attribute name="font-size">5pt</xsl:attribute>
    </xsl:attribute-set>
	
    <xsl:template match="/">
      <fo:root>
         <fo:layout-master-set>
            <fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" margin-top="5cm" margin-bottom="5cm" margin-left="2.5cm" margin-right="2.5cm">
               <fo:region-body margin-top="1cm" margin-bottom="1.5cm" />
            </fo:simple-page-master>
         </fo:layout-master-set>
         <fo:page-sequence master-reference="first">
			

            <fo:flow flow-name="xsl-region-body">
				<fo:block
    font-size="14pt" font-family="verdana" color="white"
    space-before="5mm" space-after="5mm" background-color="blue" font-weight="bold" >
						Client Info
				</fo:block>
				<fo:table text-align="left" space-before="15mm" table-layout="fixed" border-collapse="collapse">
                  <fo:table-column column-width="proportional-column-width(2)" />
                  <fo:table-column column-width="proportional-column-width(2)" />
				
				<fo:table-body>
                    <fo:table-row>
					<fo:table-cell>
					  <fo:block background-color="gray">User Name:  
					    <xsl:value-of select="/result/settings/variable[@name='$user_name$']" />
					  </fo:block>
					</fo:table-cell>
					<fo:table-cell>
					  <fo:block background-color="gray">Date Issued: 
						 <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/exec_date" />
					  </fo:block>
					</fo:table-cell>
				  </fo:table-row>
				  <fo:table-row>
					<fo:table-cell>
					  <fo:block background-color="gray">Executed By: 
						<xsl:value-of select="/result/objects[@name = 'Execution Details']/object/user_name" />
					  </fo:block>
					</fo:table-cell>
					<fo:table-cell>
					  <fo:block background-color="gray">Number of Results: 
						<xsl:value-of select="/result/objects[@name = 'Count']/object/cnt" />
					  </fo:block>
					</fo:table-cell>
				  </fo:table-row>
				</fo:table-body>
				
				
				</fo:table>
               <fo:table text-align="left" space-before="15mm" table-layout="fixed" border-collapse="collapse">
                  <fo:table-column column-width="proportional-column-width(2)" />
                  <fo:table-column column-width="proportional-column-width(2)" />
				  <fo:table-header>
				  <fo:table-row>
					<fo:table-cell background-color="blue" >
					  <fo:block font-weight="bold" color="white">User</fo:block>
					</fo:table-cell>
					<fo:table-cell background-color="blue">
					  <fo:block font-weight="bold" color="white">Client</fo:block>
					</fo:table-cell>
				  </fo:table-row>
				</fo:table-header>

                  <fo:table-body>
                     <xsl:for-each select="/result/objects[@name = 'Client Capability']/object">
                        <fo:table-row>
                           <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                              <fo:block padding="2mm" text-indent="2mm">
                                 <xsl:value-of select="/result/settings/variable[@name='$user_name$']" />
                              </fo:block>
                           </fo:table-cell>
						   <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                              <fo:block padding="2mm" text-indent="2mm">
                                 <xsl:value-of select="group_name" />
                              </fo:block>
                           </fo:table-cell>
                        </fo:table-row>
                     </xsl:for-each>
                  </fo:table-body>
               </fo:table>
            </fo:flow>
         </fo:page-sequence>
      </fo:root>
   </xsl:template>
</xsl:stylesheet>

Open in new window


This does the conversion from XML to PDF, however, the formatting is awful. The resultant PDF looks like attached result.pdf.

However, I need my output to look like


User generated image
I am not able to make out how to get rid of the additional spaces between different tables and how to make the gray area little bigger so that it does not look cluttered.

Any help is much appreaciated.
result.pdf
output.PNG
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

three things you can do
- play with padding on each block level and table cell level (top, bottom, left right or all)
- play with margins (top, bottom, left right or all)
- play with the proportional width of the columns

Here is an alternative XSLT, still ugly but you then now what the play parameters are

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="fo java" version="1.0">
    
    
    
    <xsl:variable name="m_page_height">21</xsl:variable>
    <xsl:variable name="m_page_width">29.7</xsl:variable>
    
    <xsl:attribute-set name="m_table-header">
        <xsl:attribute name="font-size">7pt</xsl:attribute>
    </xsl:attribute-set>
    <xsl:attribute-set name="m_table-cell">
        <xsl:attribute name="font-size">5pt</xsl:attribute>
    </xsl:attribute-set>
    
    <xsl:template match="/">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" margin-top="5cm" margin-bottom="5cm" margin-left="2.5cm" margin-right="2.5cm">
                    <fo:region-body margin-top="1cm" margin-bottom="1.5cm" />
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="first">
                
                
                <fo:flow flow-name="xsl-region-body">
                    <fo:block margin="0mm"
                        font-size="14pt" font-family="verdana" color="white"
                        background-color="blue" font-weight="bold"  padding="6mm">
                        Client Info
                    </fo:block>
                    <fo:table text-align="left" table-layout="fixed" border-collapse="collapse" margin="0mm">
                        <fo:table-column column-width="proportional-column-width(2)" />
                        <fo:table-column column-width="proportional-column-width(2)" />
                        
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block background-color="gray" padding="4mm">User Name:  
                                        <xsl:value-of select="/result/settings/variable[@name='$user_name$']" />
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block background-color="gray" padding="4mm">Date Issued: 
                                        <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/exec_date" />
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell>
                                    <fo:block background-color="gray" padding="4mm">Executed By: 
                                        <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/user_name" />
                                    </fo:block>
                                </fo:table-cell>
                                <fo:table-cell>
                                    <fo:block background-color="gray" padding="4mm">Number of Results: 
                                        <xsl:value-of select="/result/objects[@name = 'Count']/object/cnt" />
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                        
                        
                    </fo:table>
                    <fo:table text-align="left" table-layout="fixed" border-collapse="collapse"  margin="0mm">
                        <fo:table-column column-width="proportional-column-width(4)" />
                        <fo:table-column column-width="proportional-column-width(2)" />
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell background-color="blue" >
                                    <fo:block font-weight="bold" color="white" padding="4mm">User</fo:block>
                                </fo:table-cell>
                                <fo:table-cell background-color="blue">
                                    <fo:block font-weight="bold" color="white" padding="4mm">Client</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-header>
                        
                        <fo:table-body>
                            <xsl:for-each select="/result/objects[@name = 'Client Capability']/object">
                                <fo:table-row>
                                    <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                                        <fo:block padding="2mm" text-indent="2mm">
                                            <xsl:value-of select="/result/settings/variable[@name='$user_name$']" />
                                        </fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                                        <fo:block padding="2mm" text-indent="2mm">
                                            <xsl:value-of select="group_name" />
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Avatar of ank5

ASKER

Thank you very much.

With padding set to 4mm the middle (gray) part moves out on left and right.

User generated image
If I remove the padding, alignment falls in line, but the text looks cluttered.

User generated image
I tried inserting another row in the table, between that two rows but it does not create any space.

Is there away to create some space between the two rows

<fo:table-body>
							<fo:table-row>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">User Name:  
					    <xsl:value-of select="/result/settings/variable[@name='$user_name$']"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">Date Issued: 
						 <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/exec_date"/>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
							
							<fo:table-row>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">
									</fo:block>
								</fo:table-cell>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
														
							<fo:table-row>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">Executed By: 
						<xsl:value-of select="/result/objects[@name = 'Execution Details']/object/user_name"/>
									</fo:block>
								</fo:table-cell>
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="0mm">Number of Results: 
						<xsl:value-of select="/result/objects[@name = 'Count']/object/cnt"/>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
						</fo:table-body>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial