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 (25 → 25%)
-
"¤#,##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
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
?