Monday, January 6, 2025

OIC - XSLT - "Using format-number() to Add Leading Zeros to Single-Digit Numbers"

Use Case:

In data processing or reporting scenarios, leading zeros are crucial for ensuring consistency and accuracy in how numbers are displayed, especially for dates. 

For example, when formatting dates such as "Day 1" or "Day 9," it’s essential to present them as "01" and "09" to maintain uniformity, especially when aligning values in tables or spreadsheets. 

Using the format-number function, such as fn: format-number(ns28: Input Day, "00"), ensures that single-digit day values are always presented with a leading zero, improving both readability and data integrity in systems requiring consistent date formatting.

Code:

fn: format-number (ns28: Input Day, "00") 


OIC - XSLT - "Dynamic Payment Date Adjustment with XSLT: Handling Conditional Date Formatting"

Usecase:

This XSLT code dynamically adjusts the payment date (H_I_PAY_DATE) based on the comparison of the InputDay and the day part of the ValueDate from the source file. It formats the date by concatenating the year-month from ValueDate and the day from InputDay, or adjusts the date if necessary, ensuring proper formatting for payment processing.

Code used:

<xsl:choose>

<xsl:when test="number (ns28:InputDay) &lt; number (substring ($ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 7, 2))">

<ns23:H_I_PAY_DATE xml:id="id_76">

<xsl:value-of select="concat (substring ($ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 1, 6), fn:format-number (ns28:InputDay, '00'))"/>

</ns23:H_I_PAY_DATE>

</xsl:when>

<xsl:when test="(( number (ns28:InputDay)) &gt; ((number (substring ($ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 7, 8)))  and ((number (substring ($ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 5, 2)) -1) =0))">

<ns23:H_I_PAY_DATE xml:id="id_209">

<xsl:value-of select="concat ((number(substring ($ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 1, 4)) -1), '12', fn:format-number (number (substring (ns28:InputDay, 1, 2)), '00'))"/>

</ns23:H_I_PAY_DATE>

</xsl:when>

<xsl:when test="((number (ns28:InputDay ) &gt; number (substring ($ReadSourceFile/nsmpre:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate, 7, 8 ) )) and ((number (substring ($ReadSourceFile/nsmpre: ReadResponse/ns28: Payments/ns28: FileHeader/ns28: ValueDate, 5, 2)) - 1) != 0))">

<ns23:H_T_PAY_DATE xml:id="id_76">

<xsl:value-of xml:id="id_215" select="concat (substring ($ReadSourceFile/nsmpro: ReadResponse/ns28: Payments/ns28: FileHeader/ns28:ValueDate, 1, 4), fn: format-number ((number (substring ($ReadSourceFile/nsmpre: ReadResponse/ns28: Payments/ns28: FileHeader/ns28:ValueDate, 5, 2)) - 1), &quot;00&quot;), fn: format-number (ns28: Input Day, &quot;00&quot;) )"/>

</ns23:H_T_PAY_DATE>

</xsl:when>

<xsl:otherwise>

<ns23:H_I_PAY_DATE xml:id="id_210">

<xsl:value-of select="$ReadSourceFile/nsmpr0:ReadResponse/ns28:Payments/ns28:FileHeader/ns28:ValueDate"/>

</ns23:H_I_PAY_DATE>

</xsl:otherwise>

</xsl:choose>

Screenshot:



Featured Post

OIC - XSLT - "Using format-number() to Add Leading Zeros to Single-Digit Numbers"

Use Case: In data processing or reporting scenarios, leading zeros are crucial for ensuring consistency and accuracy in how numbers are disp...