I am transforming an XML into PDF Document using XSLT 1.0 and Groovy. When my table is rendered in PDF, XSLT is inserting Doc Link in every column, whereas my requirement is to render empty white space in the column when no link is found in the XML
<fo:table-cell border="solid 1px black">
<xsl:variable name="link"><xsl:value-of select="link/text()"/</xsl:variable>
<xsl:variable name="space" select="' '"/>
<fo:block>
<xsl:choose>
<xsl:when test="$link">
<fo:basic-link external-destination="url({$link})" color="blue" text-decoration="underline">Doc Link</fo:basic-link></xsl:when>
<xsl:otherwise><xsl:value-of select="$space"/></xsl:otherwise>
</xsl:choose>
</fo:block>
Could it be because of Groovy? If yes how can I fix it?
As #dave pointed out, you've fallen into the trap I described earlier today in Can we have multiple script function in for <xsl:value-of /> element
You need to replace
<xsl:variable name="link"><xsl:value-of select="link/text()"/</xsl:variable>
with
<xsl:variable name="link" select="link/text()"/>
I don't know why this mistake is so common, since the correct code is much shorter and simpler than the incorrect code.
I have a xml document which looks something like this:
<chapter>
<para>Just a random text<cross-ref refid="1234">Abb. 1.0</cross-ref>Some more text</para>
<section-title>Title</section-title>
<para>and more text text ext<cross-ref refif="1234">Abb 1.0</cross-ref>more more more</para>
</chapter>
As you can see there are two cross-ref elements inside paragraphs. They can occur basically everywhere and are somewhat identified by their refid(but not uniquely). What i am currently trying to do is inserting an image (based on the refid) at the position of the first occurence while keeping the text as a caption. Every other occurence (which are not the first) should just be inline texts containing an internal basic-link to that inserted image.
My current solution is:
<xsl:template match="cross-ref">
<xsl:choose>
<xsl:when test="position() = 1">
<fo:block text-align="center" id="{#refid}">
<xsl:variable name="refVar" select="#refid"/>
<xsl:variable name="imageName" select="/chapter/floats/figure[#id=$refVar]/link/#locator" />
<fo:external-graphic src="url({concat($imageName, '.jpg')})" />
<fo:block text-align="center" xsl:use-attribute-sets="lit-para">
<xsl:value-of select="current()" />
</fo:block>
</fo:block>
</xsl:when>
<xsl:otherwise>
<fo:basic-link internal-destination="{#refid}">
<xsl:value-of select="current()" />
</fo:basic-link>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
It does work on some cases, but since position() is not always 1 some images are not getting inserted correctly. What are my options?
Thank you!
EDIT: I should clarify. The image should get inserted at the first occurence of a "new" refid. Thus. Each refid only has one image and every other cross-refelement with the same refid points to that image
You have to change the test in your xsl:when, so that it is true only for the first occurrence of each #ref-id value; in other words, you have to check that no preceding cross-ref element has the same #ref-id:
<xsl:when test="not(preceding::cross-ref[#ref-id = current()/#ref_id])">
...
If you are using XSLT 2.0 or XSLT 3.0, if you add an xsl:key as a top-level element:
<xsl:key name="cross-ref" match="cross-ref" use="#refid" />
then you can change your xsl:when to:
<xsl:when test=". is key('cross-ref', #refid)[1]">
This works because key() returns nodes in document order (https://www.w3.org/TR/xslt20/#keys). This is potentially quicker (on large documents) than using the preceding axis, but to be sure you'd have to test it by running on your documents with your XSLT processor.
If you're using XSLT 1.0, you'd have to do it using a Meunchian Grouping-like trick:
<xsl:when test="count(. | key('cross-ref', #refid)[1]) = 1">
but that is much less readable than the XSLT 2.0 version.
Context
I am creating an XSL-FO document to convert my XML text to PDF.
In the XSL-FO, I have two consecutive inline elements, I would like a white space between them:
<fo:block>
<xsl:number/> <xsl:value-of select="#title"/>
</fo:block>
The expected result would be:
1 Introduction
Instead, I get
1Introduction
It seem XML do not consider this white space.
Attempts
I have tried several possible solutions, without success:
<fo:block>
<xsl:number/><fo:inline white-space="pre"> </fo:inline><xsl:value-of select="#title"/>
</fo:block>
or
<fo:block>
<xsl:number/><fo:inline margin-left="0.5cm"><xsl:value-of select="#title"/></fo:inline>
</fo:block>
None of those ideas produce an acceptable result.
The question:
How to include a white space between two (inline) elements?
Try:
<fo:block>
<xsl:number/>
<xsl:text> </xsl:text>
<xsl:value-of select="#title"/>
</fo:block>
Or:
<fo:block>
<xsl:number/>
<xsl:value-of select="concat(' ', #title)"/>
</fo:block>
The problem with
<fo:inline white-space="pre"> </fo:inline>
is that by default all whitespace-only text nodes within a stylesheet are stripped out, with the exception of those inside xsl:text elements. You can override this with xml:space="preserve"
<fo:inline xml:space="preserve" white-space="pre"> </fo:inline>
All whitespace text nodes that are descendants of an element with this attribute will be kept. Note that unlike normal namespaces you don't need to (and indeed are not allowed to) declare the xml: namespace prefix.
You can also use the following:
I have a xml document with chapters, and sub-chapters.
I have created an XSL-FO to convert the document to PDF with apache-fop. In the PDF, chapters begin always in a new page using "break-before".
I would like sub-chapters to only start on a page if there is at least 5-10 lines free: sub-chapters do not need to begin on a new page, but it is ugly to have a title in the last line and the first paragraph in the next page.
Any idea how to perform that?
Very simple example of XML file:
<document>
<chapter title="Intro">
<sub-chapter title="any-sub-title">
Any text here
</sub-chapter>
</chapter>
</document>
XSL-FO section:
...
<xsl:for-each select="chapter">
<fo:block font-weight="bold" break-before="odd-page">
<xsl:value-of select="#title"/>
</fo:block>
<xsl:apply-templates/>
</xsl:for-each>
...
<xsl:template match="sub-chapter">
<fo:block font-weight="bold">
<xsl:value-of select="#title"/>
</fo:block>
<xsl:apply-templates/>
</xsl:template>
What I think you are looking for is widow and orphan protection. With widows and orphans, you specify the number of lines in a block that cannot be left alone on one page.
<fo:block widows="4" orphans="4">
your content here.
</fo:block>
You might get a similar behavior with the keep-together or keep-with-next attributes. See the link for a quick how-to.
I am using XSL FO to generate a PDF file containing a table with information. One of these columns is a "Description" column. An example of a string that I am populating one of these Description fields with is as follows:
This is an example Description.<br/>List item 1<br/>List item 2<br/>List item 3<br/>List item 4
Inside the table cell that corresponds to this Description, I would like the output to display as such:
This is an example Description.
List item 1
List item 2
List item 3
List item 4
I've learned from searching elsewhere that you can make line breaks in XSL FO using an <fo:block></fo:block> within another <fo:block> element. Therefore, even before I parse the XML with my XSL stylesheet, I replace all occurrences of <br/> with <fo:block/>, so that the literal value of the string now looks like:
This is an example Description.<fo:block/>List item 1<fo:block/>List item 2<fo:block/>List item 3<fo:block/>List item 4
The problem arises when the Description string I am using is obtained using <xsl:value-of>, example as follows:
<fo:block>
<xsl:value-of select="descriptionStr"/>
</fo:block>
In which case, the value that gets output to my PDF document is the literal value, so it looks exactly like the previous example with all the <fo:block/> literals. I've tried manually hard-coding the <fo:block/> in the middle of another string, and it displays correctly. E.g. if I write inside my stylesheet:
<fo:block>Te<fo:block/>st</fo:block>
It will display correctly as:
Te
st
But this does not seem to happen when the <fo:block/> is inside the value of an <xsl:value-of select=""/> statement. I've tried searching for this on SO as well as Google, etc. to no avail. Any advice or help will be greatly appreciated. Thank you!
You could also replace <br/> with
and add a linefeed-treatment="preserve" attribute to your <fo:block>.
Something like:
<fo:block linefeed-treatment="preserve">This is an example Description.
List item 1
List item 2
List item 3
List item 4</fo:block>
Edit
Some users may need to use \n instead of
depending on how they are creating the XML. See Retain the
during xml marshalling for more details.
This helped me and should be simplest solution (working with Apache FOP 1.1):
Why not replace your <br/> with Unicode character called line separator.
<xsl:template match="br">
<xsl:value-of select="'
'"/>
</xsl:template>
See https://en.wikipedia.org/wiki/Newline#Unicode
The following code worked:
<fo:block white-space-collapse="false"
white-space-treatment="preserve"
font-size="0pt" line-height="15px">.</fo:block>
It makes the xsl processor thinks this block contains a line of text, which actually has a 0pt font size.
You can customize line height by providing your own value.
You shouldn't use xsl:value-of instruction but xsl:apply-templates instead: for built-in rule for text node will just output their string value, and for empty br element you could declare a rule matching descriptionStr/br or descriptionStr//br (depending your input) in order to transform to empty fo:block.
Generating strings containing escaped XML markup is seldom the right answer, but if that's what you have to work with, then for input like this:
<Description><![CDATA[This is an example Description.<br/>List item 1<br/>List item 2<br/>List item 3<br/>List item 4]]></Description>
if you're using XSLT 2.0, you can use xsl:analyze-string to get the empty fo:block that you originally wanted:
<xsl:template match="Description">
<fo:block>
<xsl:analyze-string select="." regex="<br/>">
<xsl:matching-substring>
<fo:block />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</fo:block>
</xsl:template>
but if you are using XSLT 2.0, you can more concisely use linefeed-treatment="preserve" as per #Daniel Haley and use replace() to insert the linefeeds:
<xsl:template match="Description">
<fo:block linefeed-treatment="preserve">
<xsl:value-of select="replace(., '<br/>', '
')" />
</fo:block>
</xsl:template>
If you are using XSLT 1.0, you can recurse your way through the string:
<xsl:template match="Description">
<fo:block linefeed-treatment="preserve">
<xsl:call-template name="replace-br" />
</fo:block>
</xsl:template>
<xsl:template name="replace-br">
<xsl:param name="text" select="." />
<xsl:choose>
<xsl:when test="not(contains($text, '<br/>'))">
<xsl:value-of select="$text" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($text, '<br/>')"/>
<xsl:text>
</xsl:text> <!-- or <fo:block /> -->
<xsl:call-template name="replace-br">
<xsl:with-param name="text" select="substring-after($text, '<br/>')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Try this:
<fo:block><fo:inline color="transparent">x</fo:inline></fo:block>
This code adds a block which contains transparent text, making it look like a new line.
Try using linefeed-treatment="preserve" and \n instead of <br> for a new line.
<fo:block linefeed-treatment="preserve" >
<xsl:value-of select="Description" />
</fo:block>
For XSLT 1.0 I'm using my XSLT Line-Break Template on GitHub.
For XSL-FO it supports
Line breaks
Line delimiters (vs Line breaks)
Series of pointers in a row
Ignore Pointer Repetitions (disable the Series of pointers in a row)
Any string as a pointer to insert a break or a delimiter ("\n" is default)
Line delimiters' height
Default Line delimiter height from a current font size.
Auto ignoring of the "\r" char when searching a break place.
Added support for XSLT 2.0 for a seamless migration.
something else...
For XSLT 2.0 and later consider to use approaches like
XSLT 2.0 xsl:analyze-string (RegEx)
XPath 2.0 tokenize + XSLT (RegEx)
passing sequences as a template parameter (XSLT 2.0)
and so on
I usually use an empty block with a height that can be changed if I need more or less space:
<fo:block padding-top="5mm" />
I know this isn't the best looking solution but it's funtional.
I had a text block that looks like this
<fo:table-cell display-align="right">
<fo:block font-size="40pt" text-align="right">
<xsl:text> Text 1 </xsl:text>
<fo:block> </fo:block>
<xsl:text> Text2 </xsl:text>
<fo:block> </fo:block>
<xsl:text> Text 3</xsl:text>
</fo:block>
NB: note the empty
</fo:block> on it's own is not a direct substitute for <br/> <br/> is an html unpaired abberation that has no direct equivalent in xsl:fo
</fo:block> just means end of block. If you scatter them through your text you wont have valid xml, and your xsl processor will sick up errors.
For the line break formatting you want, each block will occur on a new line. You need a <fo:block> start block and </fo:block> end block pair for each line.