Formatting DateTime in XSLT - xslt

How can I change below two fields, format of dateTime in XSLT.
DateTime format
<startdate>2002-05-30T09:30:10+06:00</startdate>
<MidDate>2002-05-30T09:30:10+06:00</MidDate>
I needed as:
<startdate>2002-05-30 09:30:10</startdate>
<MidDate>2002-05-30 9:30</MidDate>

Within XSLT 1.0 you can use substring() as follows:
<xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 1, 10)" /><xsl:text> </xsl:text><xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 12, 8)" />
Above will output:
2002-05-30 09:30:10
If you don't want the seconds attached, you simply adjust the substring a little:
<xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 1, 10)" /><xsl:text> </xsl:text><xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 12, 5)" />
Which will output:
2002-05-30 9:30
If you are able to use XSLT 2.0, you can use format-date() as described in the standard documentation: Formatting Date and Times
<xsl:value-of select="format-date('2002-05-30T09:30:10+06:00', '[Y01]-[M01]-[D01] [H]:[m]:[s]')" />

Related

XSLT format-number with comma for indian price

I want to display a price for India, like this:
5,55,555
And not
555,555
There should be no decimal. There should be a comma, like this:
1,000 one thousand
10,000 ten thousand
1,00,000 one lakh
My code:
<Price>555555</Price>
<xsl:decimal-format name="Format_INR" grouping-separator="," />
<xsl:value-of select="format-number(Price, '#,##,###', 'Format_INR')" />
But it displays
555,555
What did I do wrong?
Thank you for your help.
The XSLT 2.0 specification of format-number() allows irregular grouping separators as in your example.
The XSLT 1.0 specification is based on the Java specification of DecimalFormat, which requires regular intervals between grouping separators.
(To be more precise: the JDK 7 spec requires regular intervals, or at any rate, it treats the last interval as the one to be used: (the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####". But the XSLT 1.0 spec refers specifically to JDK 1.1.8, which is pretty-well unobtainable nowadays; my recollection is that it was very vague on such questions, and later versions of the JDK specification essentially documented the bugs in the initial implementation. To the extent that JDK 1.1.8 was vague, XSLT 1.0 implementations are free to do their own thing.)
As already mentioned, in XSLT 2.0 you can use:
<xsl:value-of select="format-number(Price, '#,##,###')" />
This will accommodate numbers up to 9,999,999. Above that, you need to add more separators, e.g.:
<xsl:value-of select="format-number(Price, '##,##,##,###')" />
will work for numbers up to 999,999,999 and so on.
In XSLT 1.0 you can do:
<xsl:choose>
<xsl:when test="Price >= 1000">
<xsl:value-of select="format-number(floor(Price div 1000), '#,##')" />
<xsl:text>,</xsl:text>
<xsl:value-of select="format-number(Price mod 1000, '000')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(Price, '#,###')" />
</xsl:otherwise>
</xsl:choose>
This will work for any magnitude of Price. If you need to reuse this, consider making it a named template.
Note that neither method requires you to define a xsl:decimal-format.

Data transformation in XSLT

I have a filed value like "+000002030" need to convert it to "20.30" how can i do this using XSLT. This value "+000002030" is the dynamic one any value it can come.Please let me know how we can convert it.
In XSLT 1.0, use:
<xsl:value-of select="format-number(translate(value, '+', '') div 100, '#.00')"/>
To make this future-proof, use:
<xsl:value-of select="format-number(number(translate(value, '+', '')) div 100, '#.00')"/>
This will work the same in both XSLT 1.0 and XSLT 2.0.
A solution in XSLT-2.0 would be
<xsl:value-of select="format-number(number('+000002030') div 100, '#.00')"/>

Convert 12 hour format date into 24 hour in XSLT1.0 or XSLT2.0

I need to convert the date from 12 hour format to 24 hour format.
Input:
01/27/2016 07:01:36 PM
Expected output:
201601271901(YYYYMMDDHHMM)
I have used format-dateTime() function in my code ,I am getting error
<xsl:value-of select="format-dateTime(part_need/promised_dt,'[Y0001][M01][D01][H01][m01]')"/>
Error:
Description: FORG0001: Invalid dateTime value "01/27/2016 07:01:36 PM" (Non-numeric year component)
Please help on this issue
Your input is not a valid ISO 8601 date/time, so you cannot use the built-in date/time functions on it.
Try instead something like (XSLT 2.0):
<xsl:template match="inputdate">
<xsl:copy>
<xsl:variable name="dte" select="tokenize(.,'/|\s|:')" />
<xsl:value-of select="$dte[3]" />
<xsl:value-of select="$dte[1]" />
<xsl:value-of select="$dte[2]" />
<xsl:variable name="h24" select="xs:integer($dte[4]) mod 12 + 12 * xs:integer($dte[7]='PM')" />
<xsl:value-of select="format-number($h24, '00')" />
<xsl:value-of select="$dte[5]" />
</xsl:copy>
</xsl:template>
Note that this assumes your days are zero-padded to two digits (as are your months).
If you need to use this in several places, consider turning it into a function.
format-dateTime takes an xs:dateTime? as the first parameter. The part_needed/promised_dt is a node.
If you have the date-time in the standard ISO format (e.g. "2006-01-27T19:01:36"), you can use xs:dateTime(part_needed/promised_dt).
Saxon does not have a non-standard date-time parser helper, so you would need to use the xs:dateTime(xs:date(year,month,day), xs:time(hours, minutes, seconds)) constructor and use something like substring(part_needed/promised_dt,1,2) to get each date/time part.

converting xsd date format using xslt

The following is the xsd format I've currently got:
2012-08-23T00:05:27Z
I want to convert this into the following xsl format:
08/23/2012 05:27 AM
xsl:value-of select="#CreatedDate" />
CreatedDate is my variable.
Assuming this is about XSLT (which I am not at all sure about), the following code:
<xsl:value-of select="concat(
substring(#CreatedDate, 6, 2), '/',
substring(#CreatedDate, 9, 2), '/',
substring(#CreatedDate, 1, 4)
)"/>
will produce a result of "08/23/2012". The rest is left as an exercise for the reader. :-)

XSLT Date Comparisons

<xsl:variable name="date1" select="2011-10-05"/>
<xsl:variable name="date2" select="2011-10-05"/>
<xsl:variable name="date3" select="2011-10-06"/>
<xsl:if test="$date2 = $date1 or $date2 < $date1">
..do something
</xsl:if>
<xsl:if test="$date3 = $date1 or $date3 > $date1">
.. do something
</xsl:if>
Both should evaluate true, but the second if doesn't. For the life of me I can't comprehended why!
In the actual transform the dates themselves are being drawn from an XML document but debugging through VS2010 i can see the values are as above.
Must be something fairly fundamental i'm doing wrong - any help would be brilliant!
I tried this in Oxygen/XML... select="2011-10-05 is being interpreted as an arithmetic expression, giving the value 1996 (2011 minus 10 minus 5) and "2011-10-06" is intrepreted as 1995.
What you want is
<xsl:variable name="date1" select="'2011-10-05'"/>
<xsl:variable name="date2" select="'2011-10-05'"/>
<xsl:variable name="date3" select="'2011-10-06'"/>
Note the extra single quotes.
From the XSLT 1.0 Specification:
If the variable-binding element has a select attribute, then the value
of the attribute must be an expression and the value of the variable
is the object that results from evaluating the expression.