I want to highlight certain words in a pdf generated with apache-fop. Because both the words to highlight as well as the input-text are dynamic, I thought calling a java function that returns xsl inline element is the easiest way:
<xsl:variable name="lines" select="ext:highlight(.)"/>
<xsl:for-each select="$lines">
<fo:block><xsl:value-of disable-output-escaping="yes" select="."/></fo:block>
</xsl:for-each>
public String highlight(String input) {
for (String toHighlight : wordsToHighlight) {
input = input.replaceAll(toHighlight, "<fo:inline background-color=\"yellow\">toHightlight</fo:inline>");
}
return input;
}
Unfortunately, the returned inline element is put literally in the pdf. What am I missing?
Also, I've looked into XSLT XML: highlight a search word in search results, but it becomes too complicated in my case.
Minimal reproducible example:
@Data
public class Example {
private Set<String> keywords = Set.of("fox", "brown");
private String inputText = "The quick brown fox jumped over the fence.";
}
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="example" />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="example">
<xsl:apply-templates select="inputText" />
</xsl:template>
<xsl:variable name="dictionary">
<entry keyword="fox" />
<entry keyword="brown" />
</xsl:variable>
<xsl:template match="inputText">
<fo:block>
<xsl:call-template name="multi-hilite">
<xsl:with-param name="string" select="." />
<xsl:with-param name="entries" select="exsl:node-set($dictionary)/entry" />
</xsl:call-template>
</fo:block>
</xsl:template>
<xsl:template name="multi-hilite">
<xsl:param name="string"/>
<xsl:param name="entries"/>
<xsl:choose>
<xsl:when test="$entries">
<xsl:call-template name="multi-hilite">
<xsl:with-param name="string">
<xsl:call-template name="hilite">
<xsl:with-param name="text" select="$string" />
<xsl:with-param name="search-string" select="$entries[1]/@keyword" />
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="entries" select="$entries[position() > 1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="hilite">
<xsl:param name="text"/>
<xsl:param name="search-string"/>
<xsl:choose>
<xsl:when test="contains($text, $search-string)">
<xsl:value-of select="substring-before($text, $search-string)"/>
<fo:inline background-color="#eeee00">
<xsl:value-of select="$search-string"/>
</fo:inline>
<xsl:call-template name="hilite">
<xsl:with-param name="text" select="substring-after($text, $search-string)"/>
<xsl:with-param name="search-string" select="$search-string"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>