fo:block next to fo:table in xsl - xslt

how can I put a fo:block with a text like:
<fo:block text-align="right" font-size="48pt">
HALLO
</fo:block>
side by side / next to a my fo:table in the xsl file.
My XSL File looks so:
<?xml version="1.0" encoding="iso-8859-1"?>
<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" xmlns:date="java.util.Date" xmlns:sf="java.text.SimpleDateFormat"
exclude-result-prefixes="java" version="1.0">
....
....
...
<xsl:template match="Order">
<fo:table border="0.5pt solid">
<fo:table-column column-width="5cm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block font-size="10pt">
delivery:
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-weight="bold" font-size="12pt">
<xsl:value-of select="#DeliveryTime"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:stylesheet>
Next to the table above in my xsl sheet I want to put the HALLO text.

If you really want this, you can specify a block-container that contains two block-containers with absolute positions placed side by side. The table goes in one, the text goes in the other.
But it's probably simpler to extend the table across the full page width and place the 'Hallo' text inside a cell, and span/merge cells if you need more room.

Related

First row of a table on a new page

I have a requirement that the first row on that page of a table (not the header) is different from the rest. I can't figure out if it can be done with markers or if it's even possible with just FO.
I basically have a legend for a graphic that would be tagged something like:
<row>
<callout>1</callout>
<name>Cog</name>
<description>Super important for stuff</description>
</row>
<row>
<callout>2</callout>
<name>Sprocket</name>
<description>Not nearly as important for stuff</description>
</row>
<row>
<callout>3</callout>
<name>Sprigot</name>
<description>I'm not creative</description>
</row>
<row>
<callout>4</callout>
<name>Last One</name>
<description>Blah blah blah</description>
</row>
The table would output as (X is a count for the number of tables):
Callout
Name
Desc
X - 1
Cog
Super important for stuff
   - 2
Sprocket
Not nearly as important for stuff
   - 3
Sprigot
I'm not creative
page break
Callout
Name
Desc
X - 4
Last One
Blah blah blah
The count only shows up on the first row on that page, but may obviously show up multiple times if the table spans multiple pages. Is there any way to get it to output like that? I'm using XSL 2.0, Saxon 9, and Apache FOP 2.7.
<xsl:template match="table">
<fo:table>
<fo:table-column column-width="20%"/>
<fo:table-column column-width="30%"/>
<fo:table-column column-width="40%"/>
<fo:table-header>
<fo:table-row>
<fo:table-cell>
<fo:block>Callout</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Name</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>Desc</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates/>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="row">
<fo:row>
<xsl:apply-templates/>
</fo:row>
</xsl:template>
<xsl:template match="callout">
<fo:table-cell>
<!-- The code needs to go here to get the count to appear only if this row is the first row on the page. -->
<fo:block>
<xsl:text> - </xsl:text>
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="name">
<fo:table-cell>
<fo:block>
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="description">
<fo:table-cell>
<fo:block>
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</xsl:template>

include chunk of xsl

I have a chunk of XSL code (along with Apache fop), that i want to reuse:
XSL version and schema used
<xsl:stylesheet version="3.0" xml:lang="en"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:exsl="http://exslt.org/common"
xmlns:java="http://xml.apache.org/xslt/java"
exclude-result-prefixes="java"
xmlns:map="xalan://java.util.HashMap" extension-element-prefixes="map">
Chunk to reuse:
<xsl:choose>
<xsl:when test="map:get($etiquetas,'datosMensajeLabel')" />
<fo:table-cell padding="2pt" border-top-color="black"
border-top-width="0.5pt" border-top-style="solid"
background-color="grey">
<fo:block text-align="start" color="white" font-size="12pt">
<xsl:value-of
select="map:put($etiquetas,'datosMensajeLabel','')" />
<xsl:value-of
select="map:get($etiquetas,'datosMensajeLabel')" />
</fo:block>
</fo:table-cell>
</xsl:choose>
<xsl:otherwhise>
<fo:table-cell padding="2pt" background-color="grey">
<fo:block text-align="start" color="white" font-size="12pt">
<xsl:value-of
select="map:get($etiquetas,'datosMensajeLabel')" />
</fo:block>
</fo:table-cell>
</xsl:otherwhise>
I tried so far:
Declare and use named templates: it fails since table-cell breaks the Apache fop schema where template is declared
Put that chunk of code in another file and use import or include: it's of no use as it's not used as a child of xsl:stylesheet
The reason you are getting the error fo:table-cell not allowed in that position is that the preceding xsl:when element is closed and therefore not wrapping the <fo:table-cell> element.
xsl:choose only allows either xsl:when or xsl:otherwise as direct children and thus complains when it finds <fo:table-cell>.
Wrapping xsl:when around <fo:table-cell> should eliminate the error message.
By the way, you have misspelt xsl:otherwise

Need to scale images to fit using XSL FO

I am working on a project in which we have charts and a table exported to PDF. We are using Apache FOP for the PDF generation. The charts are in SVG and display fine if there's only one. However, we will have cases in which there are up to four, and in these instances only three of the four are fitting on the generated PDF. I need all images to fit regardless of number. I'm afraid simply putting the page in landscape will be a temporary solution for when we need even more to fit. The sizes of each chart are set in pixels from the svg generated in the client by the Highcharts library. It appears as though the images are inserted into the pdf without any scaling, even though I set attributes for the instream-foreign-object per an example seen here: Scaling images using scale-to-fit
So I need each chart to "shrink to fit". I have tried setting the length and width of the element that encloses each element that contains an instream-foreign object (I am also including my xsl). Within the instream-foreign -object I am assigning the svg width & height to the values from the actual chart svg. I thought maybe this was the problem, but if I don't include these dimensions the charts don't appear in the pdf. I also tried setting the scaling attribute to "non-uniform" per the question here: Scale images with fixed height and the results were no better. It actually stretched a chart across the whole page thus displaying only one of four. Here's my xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="exportPage">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="exportPage">
<fo:flow flow-name="xsl-region-body">
<fo:block id="mainTitleBlock" font-family="arial, helvetica, sans-serif" color="#5c068c" font-weight="bold">
<xsl:value-of select="exports/export/charts/#mainTitle"/>
</fo:block>
<fo:block id="timestampBlock" font-family="arial, helvetica, sans-serif" font-size="small" color="#6D869F">
<xsl:value-of select="exports/export/charts/#timeStamp"/>
</fo:block>
<!-- This is where the charts go -->
<!-- Within this block will be an inline element for each chart-->
<fo:block id="chartBlock" width="8.25in" height="6in">
<xsl:apply-templates select="exports/export/charts/chart"/>
</fo:block>
<fo:block id="tableBlock" >
<xsl:apply-templates select="exports/export/table"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- Creates the table -->
<xsl:template match="table">
<fo:table table-layout="fixed" width="100%" >
<fo:table-header>
<fo:table-row>
<xsl:apply-templates select="tblRow[position() = 1]"/>
</fo:table-row>
</fo:table-header>
<fo:table-body>
<xsl:apply-templates select="tblRow[position() > 1]"/>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="hdrCell">
<fo:table-cell background-color="#666" border-right-style="solid" border-right-width="1px" border-right-color="white" empty-cells="show">
<fo:block color="white" font-family="arial, helvetica, sans-serif" font-size="x-small"><xsl:value-of select="."/></fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="tblCell">
<fo:table-cell border-bottom-style="solid" border-bottom-width="1px" border-bottom-color="#E3E3E3" >
<fo:block color="#7E7E7E" font-family="arial, helvetica, sans-serif" font-size="x-small"><xsl:value-of select="."/></fo:block>
</fo:table-cell>
</xsl:template >
<xsl:template match="tblRow[position() > 1]">
<fo:table-row>
<xsl:apply-templates />
</fo:table-row>
</xsl:template>
<xsl:template match="chart">
<fo:inline>
<fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg" content-width="scale-to-fit" content-height="100%" width="100%" scaling="uniform">
<svg:svg width="{svg:svg/#width}" height="{svg:svg/#height}">
<xsl:copy-of select="svg:svg"/>
</svg:svg>
</fo:instream-foreign-object>
</fo:inline>
</xsl:template>
</xsl:stylesheet>
What am I missing? Would it help to display the charts in table-cells (thus breaking the convention of not using tables for layout- the horror!)? How can I get these to scale and fit in the page?
Thanks,
Brandt
The scale property makes the chart fit in the containing block. If you haven't set dimensions for that block, the formatting engine won't know what size to scale to so it will use 100%.
I don't know what happens when you place more than one chart into one block.
You may need to put the charts in a table, one chart per column so you can set the column widths depending on the number of charts.

How to insert line breaks in a PDF generated with XSL-FO

I am generating a PDF using XSL-FO and XML. In a textbox, the user can enter data like "1", then he presses ENTER, then "2", ENTER, "3", etc. But in the XML and hence in the PDF, the output is "1234567". How can I preserve the line breaks? I already tried white-space-collapse, linefeed-treatment and white-space-treatment but that didn't help.
My XSL looks like:
<xsl:template match="AddCmt">
<fo:block keep-together="always"> Additional Comments
<fo:block-container border-style="solid" height="20mm" width="170mm" space-after="5mm">
<fo:block>
<xsl:attribute name="id">
<xsl:value-of select="../CMT_ID"/>
</xsl:attribute>
<xsl:value-of select="../ANS_CMT"/>
</fo:block>
</fo:block-container>
</fo:block>
</xsl:template>
When I enter the following:
hello
medhavi
saraswat
This is the XML I get:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type='text/xsl' href='e:\tmm-09.3\src\pmod\WorkOrder.xsl'?>
<Root>
<WorkOrders>
<Detail>Id="ANS_436‌​_FLD_1" Label="qq">qq</Detail>
<Ans Checked="0" Id="ANS_436_FLD_2" Label="ww">ww</Ans>
<ID>ANS_436_FLD</ID>
<ANS_FLD>0|0</ANS_FLD>
<CMT_ID>ANS_436_CMT‌​</CMT_ID>
<ANS_CMT>hello medhavi saraswat</ANS_CMT>
<Warning>
<Line>warning 11</Line>
<Line>22</Line>
<Line>33</Line>
<Line>44</Line>
<Line></Line>
<Line>66</Lin‌​e>
<Line>77</Line>
<Line></Line>
</Warning>
It should work with the following xml (you should add all the attributes):
<xsl:template match="AddCmt">
<fo:block keep-together="always"> Additional Comments
<fo:block-container border-style="solid" height="20mm" width="170mm" space-after="5mm">
<fo:block wrap-option="wrap" linefeed-treatment="preserve" white-space-collapse="false" white-space-treatment="preserve">
<xsl:attribute name="id">
<xsl:value-of select="../CMT_ID"/>
</xsl:attribute>
<xsl:value-of select="../ANS_CMT"/>
</fo:block>
</fo:block-container>
</fo:block>
</xsl:template>
But as I mentioned in the comments, if your XML already has no linebreaks, there's no way your PDF will. You mentioned in your question there are no linebreaks in your XML, hence no linebreaks in the PDF.
Try checking out why there are no linebreaks in the XML. If you can provide any more information (a piece of your XML, the code you use to construct the XML, ...), please edit your answer and add the information.

XSLT: Subtotals

Source XML:
<Root>
<Data>
<Code>A</Code>
<Value>10</Value>
</Data>
<Data>
<Code>A</Code>
<Value>10</Value>
</Data>
<Data>
<Code>B</Code>
<Value>10</Value>
</Data>
<Data>
<Code>A</Code>
<Value>2</Value>
</Data>
<Data>
<Code>C</Code>
<Value>10</Value>
</Data>
<Data>
<Code>A</Code>
<Value>5</Value>
</Data>
<Data>
<Code>B</Code>
<Value>4</Value>
</Data>
<Data>
<Code>A</Code>
<Value>10</Value>
</Data>
<Data>
<Code>C</Code>
<Value>10</Value>
</Data>
<Data>
<Code>B</Code>
<Value>10</Value>
</Data>
<Data>
<Code>A</Code>
<Value>10</Value>
</Data>
<Data>
<Code>C</Code>
<Value>5</Value>
</Data>
....
</Root>
XSL-FO Code:
My code(XSL-FO) contains 3 columns where each column contains the content of 'A', 'B', 'C'
<fo:table-body>
<xfd:table-row-repeat xpath="Root/Data" font-family="Arial Narrow" font-size="10pt" padding-after="0.55cm">
<xsl:if test="Code='A'">
<fo:table-cell>
<fo:block height="12pt">Value
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<xsl:value-of select="Value" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</xsl:if>
</xfd:table-row-repeat>
</fo:table-body>
Same code for each columns to display values of 'B' & 'C'
In Table-footer i've to get these subtotal of 'A','B', 'C'
<fo:table-body>
<xfd:table-row-repeat xpath="Root/Data" font-family="Arial Narrow" font-size="10pt" padding-after="0.55cm">
<xsl:if test="Code='A'">
<fo:table-cell>
<fo:block height="12pt">SubTotal
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<--Here Sum of first 15 A's. if the A's or B's or C's exceed by 15, then the table flows to 2nd Page. In that case, 1st Page table-footer shows individual subtotals of first 15 A's, 15 B's and C's. In 2nd Page, the subtotals should contain Subtotal of first 15 A's+ Succeeding A's, in the same way B's and C's -->
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</xsl:if>
</xfd:table-row-repeat>
</fo:table-body>
Here the XSL-FO code is shown for only one column(For Root/Data/Code='A'), the other 2 columns('B' & 'C') consists of same code.
Conditions in Detail:
Condition 1): when Root/Data/Code = 'A' or 'B' or 'C'
i need individual totals of 'A', 'B' and 'C' in Table-Footer individual Column.
Condition 2): inturn if individual count(Root/Data/Code) of 'A', 'B' & 'C' crosses 15. Then Page flows to 2nd Page then Table-Footer in 2nd Page needs to contains subtotal of first 15 A's + the sum of succeeding A's in the same way for B's And C's
i.e., if 20 A's, 10 B's and 25 C's are present in Source XML.
In 1st Page, Table-Footer
SubtotalI(Value of 15 A's)=
SubtotalII(Value 10 B's)=
SubtotalIII(Value 15 C's)=
In 2nd Page, Table-Footer
SubtotalI(15 A's+ next 5 A's)=
SubtotalII(Value 10 B's)= <!--No Change as count of B's is less than 15 -->
SubtotalIII(15 C's + next 10 C's)=
I'm trying this logic using xsl:key by grouping through Code tag for evaluating sum of 'A', 'B' and 'C'. As i'm new to XSLT i'm finding it too difficult to solve this logic using xsl:key. Can anyone help in solving this logic?
Thanks in Advance
There are several difficulties with what you try to achieve. Calculating subtotals at a given point is easiest actually. You just need the preceding axis and a sum to calculate it:
sum(preceding::Value[../Code = 'A'])
To include the current value as well, use the union operator like this:
sum(Value[../Code = 'A'] | preceding::Value[../Code = 'A'])
A bigger difficulty is to show a different table 'footer' on each page. Headers and footers are repeated on pages automatically, but the contents is the same across all pages which the table spans. The only solution I see is to break the table yourself, adding a different table footer each time.
By far the easiest way is to just take a fixed number of Data elements at a time, and displaying those in a separate table. You can loop through A, B and C types in one for-each, giving each value a separate row. That way the table always has the same number of rows. You can experiment with the number you can include to determine how many fit on one page.
The following code returns a table with the first 10 Data values. A, B and C values are positioned straight under each other, but you could position them left, middle, and right respectively if you like. At the bottom of the table three rows are added with subtotals for A, B and C.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="global">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="global">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table table-layout="fixed" width="150mm" border-style="solid">
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-column column-width="50mm"/>
<fo:table-body font-size="7pt">
<xsl:for-each select="/Root/Data[10 >= position()]">
<fo:table-row border-style="solid">
<fo:table-cell>
<fo:block height="12pt">
<xsl:value-of select="Code" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<xsl:value-of select="Value" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:if test="position() = last()">
<fo:table-row border-style="solid">
<fo:table-cell>
<fo:block height="12pt">Subtotal A</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<xsl:value-of select="sum(preceding::Value[../Code = 'A'] | Value[../Code = 'A'])" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row border-style="solid">
<fo:table-cell>
<fo:block height="12pt">Subtotal B</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<xsl:value-of select="sum(preceding::Value[../Code = 'B'] | Value[../Code = 'B'])" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row border-style="solid">
<fo:table-cell>
<fo:block height="12pt">Subtotal C</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt" border="0.1pt solid black" text-align="center">
<xsl:value-of select="sum(preceding::Value[../Code = 'C'] | Value[../Code = 'C'])" />
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block height="12pt">Points</fo:block>
</fo:table-cell>
</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>
You still need something to determine how many tables of n Data items you would need, and then do some recursive calls to output all of them. Hope this is sufficient for the moment to get you going again!
PS: I noticed you are using xfd prefix. That looks like you are working with the XF Designer from Ecrion. I am not very familiar with it. The above code is a plain XSLT 1.0 solution. Not sure it works in XF Designer, please let me know..