XSLT - Formatting Numbers

 In XSLT you can format numbers and dates/times, but the way you do it depends on the version of XSLT you’re using. Let’s go step by step.


1. Formatting Numbers

In XSLT 1.0, you use format-number().

Syntax

format-number(number, pattern, decimal-format-name?)
  • number → numeric value to format.

  • pattern → formatting pattern, similar to Java’s DecimalFormat.

  • decimal-format-name → optional, if you define custom decimal symbols.

Common patterns

  • "0.00" → fixed decimals (12.30)

  • "#,###.00" → with grouping (1,234.50)

  • "##%" → percentage (2525%)

  • "¤#,##0.00" → currency (¤ replaced by currency symbol if defined).

Example

<xsl:value-of select="format-number(1234.567, '#,###.00')"/>

Output:

1,234.57

2. Formatting Dates

XSLT 1.0

  • There is no built-in date formatting in XSLT 1.0.

  • You usually:

    • Use substring() and concat() to rearrange dates if they’re in ISO format (YYYY-MM-DD).

    • Or rely on extension functions provided by processors (like EXSLT or vendor-specific functions).

Example (rearranging YYYY-MM-DD → DD/MM/YYYY):

<xsl:value-of select="concat(substring(date,9,2), '/', substring(date,6,2), '/', substring(date,1,4))"/>

If <date>2025-09-23</date>Output: 23/09/2025


XSLT 2.0 and later

XSLT 2.0 introduced full support for dates and times.

  • format-date()

  • format-dateTime()

  • format-time()

Syntax

format-dateTime(xs:dateTime, picture [, language [, calendar [, country]]])

Example (XSLT 2.0+)

<xsl:value-of select="format-date(xs:date('2025-09-23'), '[D01]/[M01]/[Y0001]')"/>

Output:

23/09/2025

Another example with month name:

<xsl:value-of select="format-date(xs:date('2025-09-23'), '[D1] [MNn] [Y0001]')"/>

Output:

23 September 2025

 Summary

  • Numbers: Use format-number() (XSLT 1.0 and 2.0).

  • Dates:

    • XSLT 1.0 → no built-in support (manually use substring(), concat(), or processor extensions).

    • XSLT 2.0+ → format-date(), format-dateTime(), format-time() for full control.


 

?