Skip to main content

The PreTeXt Guide

Section 41.8 XSL 101

If you have read this far, and read carefully, you have been exposed to several key principles of writing XSL. Basically you are creating templates which the base conversion will “call” in exactly the right place. In a procedural language these might be called hooks. Besides a smooth integration with the rest of PreTeXt there are several advantages:
  • A template using a @match can apply narrowly or broadly. Witness the example above for <example> versus &EXAMPLE-LIKE;. This attribute is similar to a this pointer in an object-oriented language and the modal template is not dissimilar to a method.
  • PreTeXt has a discovery phase when it constructs the preamble. If your PreTeXt source has no <example> in it, then there will be no associated tcolorbox style added to the preamble, and the example environment will not be defined. So the preamble is exactly what your document needs, and no more (mostly). The @match attribute makes this possible.
  • You have seen named templates, which will feel familiar if you know procedural languages. While perhaps comfortable, they are way less powerful, and we noted that we may get rid of them.
  • The <xsl:import> mechanism allows us to keep base definitions and override others. So as you develop your style, you do not need to start from scratch.
Be aware that every single character that you put inside the <xsl:text> element will get copied literally into the preamble of your output, including newlines and spaces you use to indent in your XSL. Conversely, any whitespace inside your template, but between the <xsl:text> elements is ignored. So we like to use multiple <xsl:text> elements (except we were a bit lazy getting this out the door initially—do as we say, not as we do) and explicitly create newlines with the &#xa; character. Your goal is to have a preamble with no blank lines and no unintended indentation. We often create comment lines (with two leading % characters to aid with readability.
See the PreTeXt website for recommendations for books on XSL if you are encouraged to learn more.