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

asked on

Xsl-fo show PDF when no results are returned

I am using the below XSL to generate a PDF from XML

<?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="20pt" font-family="Calibri" color="white" background-color="#1f497d" text-align="center" padding="6mm">
						Customer
				</fo:block>
					<fo:table text-align="left" table-layout="fixed" 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 margin-left="4mm" margin-right="4mm">
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="4mm" font-size="14pt" font-family="Calibri"> 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="4mm" font-size="14pt" font-family="Calibri">Date: 
						 <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/exec_date"/>
									</fo:block>
								</fo:table-cell>
							</fo:table-row>
							<fo:table-row margin-left="4mm" margin-right="4mm">
								<fo:table-cell>
									<fo:block background-color="#d9d9d9" padding="4mm" font-size="14pt" font-family="Calibri">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="4mm" font-size="14pt" font-family="Calibri">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(2)"/>
						<fo:table-column column-width="proportional-column-width(2)"/>
						<fo:table-header>
							<fo:table-row>
								<fo:table-cell background-color="#1f497d">
									<fo:block color="white" padding="4mm" font-size="14pt" font-family="Calibri">User</fo:block>
								</fo:table-cell>
								<fo:table-cell background-color="#1f497d">
									<fo:block color="white" padding="4mm" font-size="14pt" font-family="Calibri">Customer</fo:block>
								</fo:table-cell>
							</fo:table-row>
						</fo:table-header>
						<fo:table-body>
							<xsl:for-each select="/result/objects[@name = 'Customer']/object">
								<fo:table-row>
									<fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
										<fo:block padding="2mm" text-indent="2mm" font-size="11pt" font-family="Calibri">
											<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" font-size="11pt" font-family="Calibri">
											<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


<?xml version='1.0' encoding='UTF-8'?>
<result>
  <settings>  
    <reportname>Customer</reportname>
  </settings>
 <!-- <objects name="Customer">
    <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="Customer" />
  <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


The PDF gets generated fine when there is data inside  
<objects name="Customer">

Open in new window


However, there are times when this element is empty (as in the example). In this case user gets an error when trying to open the PDF (as if a corrupted PDF is generated).

I would like to have the user still open the PDF, and may be show a message in the table saying 'No results found'.

Can someone please help me in achieving this?

Thank you
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

<?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:output indent="yes"/>
    
    <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="20pt" font-family="Calibri" color="white" background-color="#1f497d" text-align="center" padding="6mm">
                        Customer
                    </fo:block>
                    <fo:table text-align="left" table-layout="fixed" 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 margin-left="4mm" margin-right="4mm">
                                <fo:table-cell>
                                    <fo:block background-color="#d9d9d9" padding="4mm" font-size="14pt" font-family="Calibri"> 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="4mm" font-size="14pt" font-family="Calibri">Date: 
                                        <xsl:value-of select="/result/objects[@name = 'Execution Details']/object/exec_date"/>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row margin-left="4mm" margin-right="4mm">
                                <fo:table-cell>
                                    <fo:block background-color="#d9d9d9" padding="4mm" font-size="14pt" font-family="Calibri">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="4mm" font-size="14pt" font-family="Calibri">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(2)"/>
                        <fo:table-column column-width="proportional-column-width(2)"/>
                        <fo:table-header>
                            <fo:table-row>
                                <fo:table-cell background-color="#1f497d">
                                    <fo:block color="white" padding="4mm" font-size="14pt" font-family="Calibri">User</fo:block>
                                </fo:table-cell>
                                <fo:table-cell background-color="#1f497d">
                                    <fo:block color="white" padding="4mm" font-size="14pt" font-family="Calibri">Customer</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-header>
                        <fo:table-body>
                            <xsl:call-template name="customer-object"/>
                           </fo:table-body>
                    </fo:table>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    
    <xsl:template name="customer-object">
        <xsl:choose>
            <xsl:when test="/result/objects[@name = 'Customer']/object">
                <xsl:apply-templates select="/result/objects[@name = 'Customer']/object"/>
            </xsl:when>
            <xsl:otherwise>
                <fo:table-row>
                    <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                        <fo:block padding="2mm" text-indent="2mm" font-size="11pt" font-family="Calibri">
                            <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" font-size="11pt" font-family="Calibri">
                            <xsl:text>NO DATA FOUND IN CUSTOMER OBJECT</xsl:text>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="objects[@name = 'Customer']/object">
              <fo:table-row>
                <fo:table-cell border-width="0.2mm" border-style="solid" text-align="left">
                    <fo:block padding="2mm" text-indent="2mm" font-size="11pt" font-family="Calibri">
                        <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" font-size="11pt" font-family="Calibri">
                        <xsl:value-of select="group_name"/>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
    </xsl:template>
    
</xsl:stylesheet>

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