Search code examples
xsltsaxonapache-fop

XSLT - Add some spacing before punctuation


I use Apache FOP version 2.8 with saxonHE12 available.

Is there a way in XSLT to add an additional space BEFORE the punctuation character when punctuating (, ? ! etc)?

I have seen that there is something from Antennahouse axf:space-before-punctuation but as far as I know I can't use it as FOP user.

Is there any other way to add, for example, an extra space of say 0.2 cm before each comma?

Example:

The sun was setting, casting a warm golden glow over the horizon.

Output after adding some spacing:

The sun was setting<spacing of 0.2 cm>, casting a warm golden glow over the horizon<spacing of 0.2 cm>.

Solution

  • As your output format is HTML, you can enclose the punctuation characters in span elements and style them with CSS:

    <xsl:template match="text()">
      <xsl:analyze-string select="." regex="[,.!?]">
        <xsl:matching-substring>
          <span style="padding-left:2px"><xsl:value-of select="."/></span>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:value-of select="."/>
        </xsl:non-matching-substring>
      </xsl:analyze-string>
    </xsl:template>
    

    remark: It's my first time making use of xsl:analyze-string

    The output would be something like:

    The sun was setting<span style="padding-left:2px">,</span> casting a warm golden glow over the horizon<span style="padding-left:2px">.</span>