Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-use an xsl-fo block?

Tags:

xslt

xsl-fo

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?

like image 792
Martin Ba Avatar asked Oct 23 '25 02:10

Martin Ba


1 Answers

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).

like image 198
Mathias Müller Avatar answered Oct 26 '25 02:10

Mathias Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!