How can I format pubDate to HH:MM AM/PM? - xslt

Is there an easy way to format an RSS pubDate
<pubDate>Thu, 28 May 2015 08:00:00 -0400</pubDate>
into:
8:00AM
I'm not all that familiar with date formatting, and I can't for the life of me recall what that time format is, so Google isn't helping me any.

Try it this way:
XSLT 1.0
<xsl:template match="pubDate">
<xsl:variable name="h" select="substring(., 18, 2)"/>
<xsl:variable name="m" select="substring(., 21, 2)"/>
<xsl:variable name="h12" select="($h + 11) mod 12 + 1"/>
<xsl:variable name="am.pm" select="substring('AMPM', 1 + 2*(number($h) > 11), 2)"/>
<xsl:value-of select="concat($h12, ':', $m, $am.pm)"/>
</xsl:template>

General date parsing is quite complex. There were some discussions to add the opposite of fn:format-date() to the standard XPath library 3.0, but did not succeed because of the complexity of the task in the generic case.
I am afraid you are limited to string manipulation here, which in your case is not too bad. For instance:
<xsl:variable name="hours" select="xs:integer(substring($input, 18, 2))"/>
<xsl:variable name="minutes" select="substring($input, 21, 2)"/>
<xsl:sequence select="
if ( $hours ge 12 ) then
concat($hours - 12, ':', $minutes, 'PM')
else
concat($hours, ':', $minutes, 'AM')"/>
Error management has not been added, up to you to add some, depending on how good and homogeneous is your input.

Related

Conversion of calendar date to Julian date

Please help me with the code of converting given calendar date to Julian date(YYYYMMM) 7 bytes format using xslt please...
Thanks in advance !
The JD Edwards date format (which is often referred to incorrectly as the JDE Julian Date format) is a CYYDDD format, where:
C is a number denoting the century: a value of 0 represents years between 1900 and 1999, a value of 1 years from 2000 to 2099, and so on;
YY denotes the year in the given century;
DDD is the day number in the given year.
Thus, for example, the date of 2019-07-02 would be represented as 119183.
To convert a date in YYYY-MM-DD format to JD Edwards date format using XSLT 1.0, you can use:
<xsl:template name="date-to-JDE">
<xsl:param name="date"/>
<xsl:variable name="year" select="substring($date, 1, 4)"/>
<xsl:variable name="month" select="substring($date, 6, 2)"/>
<xsl:variable name="day" select="substring($date, 9, 2)"/>
<xsl:variable name="C" select="floor($year div 100) - 19"/>
<xsl:variable name="YY" select="$year mod 100"/>
<xsl:variable name="leap" select="not($year mod 4) and $year mod 100 or not($year mod 400)" />
<xsl:variable name="elapsed-months" select="substring('000031059090120151181212243273304334', 3 * ($month - 1) + 1, 3)"/>
<xsl:variable name="DDD" select="$elapsed-months + ($month > 2 and $leap) + $day"/>
<xsl:value-of select="$C * 100000 + $YY * 1000 + $DDD"/>
</xsl:template>
Demo: https://xsltfiddle.liberty-development.net/jyRYYj8
Added:
If you actually need YYYYDDD, then you can simplify the above to:
<xsl:template name="date-to-YYYYDDD">
<xsl:param name="date"/>
<xsl:variable name="year" select="substring($date, 1, 4)"/>
<xsl:variable name="month" select="substring($date, 6, 2)"/>
<xsl:variable name="day" select="substring($date, 9, 2)"/>
<xsl:variable name="leap" select="not($year mod 4) and $year mod 100 or not($year mod 400)" />
<xsl:variable name="elapsed-months" select="substring('000031059090120151181212243273304334', 3 * ($month - 1) + 1, 3)"/>
<xsl:variable name="DDD" select="$elapsed-months + ($month > 2 and $leap) + $day"/>
<xsl:value-of select="$year * 1000 + $DDD"/>
</xsl:template>
In XSLT 2.0, format-date() with picture [Y0001][d001] should give you what you want.
If you can't use XSLT 2.0, see whether your XSLT processor supports the date/time extensions defined at www.exslt.org

Convert date format from dd/mm/yyyy to yyyy-mm-dd using xsl

First time I am using XSLT.
I need to convert date from dd/mm/yyyy to yyyy-mm-dd .
Below are my codes.
<xsl:element name="transactionDate">
<xsl:value-of select="concat(substring(col26,5,5), '-', substring(col26,3,1), '-', substring(col26,1,1))"/>
</xsl:element>
In above code I am able to convert the date from dd/mm/yyyy to yyyy-mm-dd but when the date came as double digit then its not working.
Can somebody please suggest.
Thank in advanced.
If col26 contains a date in dd/mm/yyyy format, then your expression should be:
<xsl:value-of select="concat(substring(col26, 7, 4), '-', substring(col26, 4, 2), '-', substring(col26, 1, 2))"/>
Not sure what you mean by "when the date came as double digit". The above assumes that the days and months in the input are zero-padded to 2 digits each. Otherwise the format is not dd/mm/yyyy.
Added:
if the date is coming as 2/4/2019 then its (my code )working fine
If the date can be 2/4/2019 then your format is d/m/y, not dd/mm/yyyy. To convert it to yyyy-mm-dd, try:
<transactionDate>
<xsl:variable name="d" select="substring-before(col26, '/')" />
<xsl:variable name="m" select="substring-before(substring-after(col26, '/'), '/')" />
<xsl:variable name="y" select="substring-after(substring-after(col26, '/'), '/')" />
<xsl:value-of select="format-number($y, '0000')" />
<xsl:value-of select="format-number($m, '-00')" />
<xsl:value-of select="format-number($d, '-00')" />
</transactionDate>
Added #2:
sorry to inform you that in date field the value is "20/12/2019 9:24:00 AM" ,time is also coming in csv
It is indeed regrettable that you did not reveal this information sooner.
If your input format is d/m/y h:mm:ss xm, then use:
<transactionDate>
<xsl:variable name="date" select="substring-before(col26, ' ')" />
<xsl:variable name="d" select="substring-before($date, '/')" />
<xsl:variable name="m" select="substring-before(substring-after($date, '/'), '/')" />
<xsl:variable name="y" select="substring-after(substring-after($date, '/'), '/')" />
<xsl:value-of select="format-number($y, '0000')" />
<xsl:value-of select="format-number($m, '-00')" />
<xsl:value-of select="format-number($d, '-00')" />
</transactionDate>
Demo: https://xsltfiddle.liberty-development.net/pPzifpU

XSLT 1.0 String Length - 2

I need to check the length of a field and then add . before last two
digits.
Example: the value of Amount is 0001234567, to be replaced as 00012345.67. Here string length will be 10.
But the command fails and is not able to retrieve the value from
($VARAmtLength-2) or ($VARAmtLength-1).
My code as below:
<xsl:variable name="VARAmtLength" select="string-length (ns0:Amount )"/>
<xsl:if test=" ($VARAmtLength> 0)">
<tns:Amount>
<xsl:value-of select="concat(substring(ns0:Amount, 1, ($VARAmtLength- 2)),'.', substring(ns0:Amount, ($VARAmtLength-1, 2)))"/>
</tns:Amount>
</xsl:if>
Any help?
I think your code is working fine.
Just replace this line with existing one:
<xsl:value-of select="concat(substring(ns0:Amount, 1, ($VARAmtLength - 2)),'.', substring(ns0:Amount, ($VARAmtLength - 1), 2))" />
1. There should be a space around subtraction operator '-'. Otherwise it will consider $VARAmtLength- as variable name.
2. You had misplaced round parentheses for second substring() function.
XML
<amount>0001234567</amount>
Xsl
<xsl:template match="/">
<xsl:variable name="length" select="//amount"/>
<xsl:if test="$length>0">
<amount>
<xsl:variable name="ajeet" select="concat(substring(//amount, 1, 8), '.')"/>
<xsl:variable name="kumar" select="substring(//amount, 9, 2)"/>
<xsl:value-of select="concat($ajeet, $kumar)"/>
</amount>
</xsl:if>
</xsl:template>

How can I use the value of a variable for the use property of a key in XSLT, I want to achieve use="$Variable" in the key tag;;

Suppose I have a key defined in an xslt file in SharePoint Designer 2010 as:
<xsl:key name="Years" match="/dsQueryResponse/Rows/Row" use="#Date" />
Where #Date is the column, however instead of #Date, I want to use the value of the following variable:
<xsl:variable name="VarNAme">
<xsl:choose>
<xsl:when test="string-length(#Date) = 8">
<xsl:value-of select="substring(#Date, 5, 4)"></xsl:value-of>
</xsl:when>
<xsl:when test="string-length(#Date) = 9">
<xsl:value-of select="substring(#Date, 6, 4)"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(#Date, 7, 4)"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
If there is a better way (one-liner) to get just the year from a date, I'd welcome that as well. I want to use generate-id to get distinct years (not dates, years).
<xsl:key
name="Years"
match="/dsQueryResponse/Rows/Row"
use="substring(#Date, string-length(#Date) - 3, 4)"
/>
Hint
8 - 3 = 5
9 - 3 = 6
10 - 3 = 7
;-)

SSN format in XSLT?

I could use some help creating an XSL template that will take a string of numbers (i.e., 123456789) and format that string into a Social Security Number format (i.e., 123-45-6789). I found one example on the Internet, but it seemed overcomplicated.
I'm new to XSLT, so please keep that in mind when replying. Thank you!
XSLT 1.0's string functions are a bit limited, but fortunately this isn't too hard:
Assuming < ssn >123456789< /ssn>:
<xsl:template match="ssn">
<xsl:value-of select="substring(., 0, 4)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring(., 4, 2)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring(., 6, 4)"/>
</xsl:template>
In XSLT 2.0, concat() can take more than two arguments, so it's a single line:
<xsl:template match="ssn">
<xsl:value-of select="concat(substring(., 0, 4), '-', substring(., 4, 2), '-', substring(., 6, 4))" />
</xsl:template>