Question

I'm maintaining an xsl-fo file. I'm neither good at xslt, nor at xsl-fo and I only touch that file once a year.

I noticed that there is a lot of duplication in that file. For example, I have this exact block twice (verbatim) in different contexts:

                <xsl:if test="ReleaseNote!=''">
                  <fo:block background-color="#ffeeee" padding="5mm" font-size="12pt" text-indent="0" border-style="solid" border-width="1px" border-color="red">
                    <fo:table table-layout="fixed" width="100%">
                      <fo:table-column column-width="20mm" />
                      <fo:table-column column-width="135mm" />
                      <fo:table-body>
                        <fo:table-row>
                          <fo:table-cell margin="0" padding="0" text-align="justify">
                            <fo:block text-align="justify">

                              <fo:external-graphic src="pic/warning.png" content-width="12mm" content-height="12mm" />
                            </fo:block>
                          </fo:table-cell>
                          <fo:table-cell margin="0" padding="0">
                            <fo:block linefeed-treatment="preserve">
                              <xsl:value-of select="ReleaseNote" />
                            </fo:block>
                          </fo:table-cell>
                        </fo:table-row>
                      </fo:table-body>
                    </fo:table>
                  </fo:block>
                </xsl:if></fo:block>

How do I remove this duplication? Can I pre-define this block and then re-use a "reference" to it in the xslt?

Was it helpful?

Solution

You could put this inside a named template. More details on named templates (especially parameters) in the relevant part of the specification here.

<xsl:template name="reusable-content">
   <xsl:if test="ReleaseNote!=''">
      <!--...-->
   </xsl:if>
</xsl:template>

Then, call the template where you need the content:

<xsl:call-template name="reusable-content"/>

The advantage of named templates is that the context of the calling template is preserved. In other words, any XPath expressions that worked in the original code will also work flawlessly when executed from within a named template. This is only important if the code depends on the context, as yours does:

<xsl:value-of select="ReleaseNote" />

The instruction above relies on a context where ReleaseNote is available as a child element.


Another solution to your problem would be to store reusable content in a variable. But there might be some limitations, especially if you use XSLT 1.0 (you did not reveal which version you use).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top