[xsl] emulating sprintf in XSLT

Subject: [xsl] emulating sprintf in XSLT
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 28 Aug 2022 23:06:04 -0000
Hi everyone,

If you need sprintf()-like functionality in XSLT, one approach is to use the
arrow (=>) operator to apply a series of replace() operations:

    <xsl:if test="$word-count > 0">
      <xsl:variable name="minutes" select="xs:integer(ceiling($word-count div
$words-per-minute))" as="xs:integer"/>
      <div>
        <strong>Estimated reading time:</strong>
        <xsl:value-of select="' %d %s'
          => replace('%d', string($minutes))
          => replace('%s', if ($minutes = 1) then 'minute' else 'minutes')"/>
      </div>
    </xsl:if>

Just wanted to share. Thanks!

-----
Chris Papademetrious
Tech Writer, Implementation Group
(610) 628-9718 home office
(570) 460-6078 cell

Current Thread