Keywords: xref, suffix, prefix

Problem

You have a cross-reference (usually tagged with xref) and you want to append text or graphics after the reference is resolved.

Solution

Use the xref-to-prefix or xref-to-suffix mode. These modes are called before or after the xref is being resolved. As such it makes it pretty easy to add text or graphics.

For example, the following example appends the text “[x]” to each xref, regardless where it points to:

Example 2.5. Appending Simple Text to Each Element
<xsl:template match="*" mode="xref-to-suffix">
   <xsl:text>[X]</xsl:text>
</xsl:template>

If you need the text only for specific elements (for example, only for chapters) use the match attribute:

Example 2.6. Appending Simple Text for Each Cross-Reference Pointing to a Chapter
<xsl:template match="d:chapter" mode="xref-to-suffix">
   <xsl:text>[X]</xsl:text>
</xsl:template>

Discussion

If you have to insert text before or after your reference only, you can use one of the templates described above. However, if you need to display a graphic, you need to insert FO- or HTML-specific code. For HTML, this could look like this:

Example 2.7. Adding Graphic for HTML
<xsl:template match="d:chapter" mode="xref-to-suffix">
   <img src="chapter.png" alt="Chapter"/>
</xsl:template>

If you need to add a graphic for FO, use the following template:

Example 2.8. Adding Graphic for FO
<xsl:template match="d:chapter" mode="xref-to-suffix">
   <xsl:variable name="srcfile">
     <xsl:call-template name="fo-external-image">
       <xsl:with-param name="filename" select="'chapter.png'"/>
     </xsl:call-template>
   </xsl:variable>

   <fo:external-graphic src="{$srcfile}" height="1em"/>
</xsl:template>

The same principle applies if you want to add something before your cross-reference. In this case, replace the xref-to-suffix mode with xref-to-prefix.


Project@GitHubIssue#7