SSN format in XSLT? - 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>

Related

DateTime Parsing in XSLT Xp20

I am having the Date field as below
<Input>
<date>08/26/2020</date>
</Input>
i would need to parse it as like below
<date>2020-08-26</date>
I have tried using below xpath funtion in XSLT, which is not producing any result
xp20:format-dateTime(/Input/date,'[Y0001]-[M01]-[D01]')
Any help here??
Using replace you can reorder the components, if you want to create an XSLT/XPath xs:date, additionally use the constructor function:
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="xs:date(replace(., '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$1-$2'))"/>
</xsl:copy>
</xsl:template>
What you could do is, extract sub strings, and re-join them as needed.
<date><xsl:value-of select="concat(substring-after(substring-after(date/text(),'/'),'/'), '-', substring-before(date/text(),'/'), '-', substring-before(substring-after(date/text(),'/'),'/'))" /></date>
or better use a template for this:
<xsl:template name="format_date">
<xsl:param name="date" />
<xsl:value-of select="concat( substring($date, 7, 2),'.',substring($date, 5, 2), '.', substring($date, 1, 4), ', ', substring($date, 9, 2),':',substring($date, 11, 2),'h' )" />
</xsl:template>
and pass the date as param (with-param).
which'll give you the desired <date>2020-08-26</date>

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 format pubDate to HH:MM AM/PM?

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.

Convert date in XSL

I was wondering how can you this kind of date into a normal one.
Sample xml:
I want my output to be like this: 01/03/1959 based from the sample xml. I'm using xslt version 1.0 and xpath 1.0
Well, there are no dates as such in XSLT 1.0, so just threat this as an exercise in string manipulation and write out:
<xsl:value-of select="substring(data, 5, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(data, 7, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(data, 1, 4)"/>
or, if you prefer:
<xsl:value-of select="concat(substring(data, 5, 2), '/', substring(data, 7, 2), '/', substring(data, 1, 4))"/>
how can you this kind of date into a normal one.
Actually, the "normal one" would look like this: 1959-01-03.
http://en.wikipedia.org/wiki/ISO_8601

Best way to remove a leading zero after formatting a date in xslt 1.0

I've got a date format template that I'm passing a date value to in the format YYYYMMDD
The template is the following:
<xsl:template name="formatDate">
<xsl:param name="date" />
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 5, 2)" />
<xsl:variable name="day" select="substring($date, 7, 2)" />
<xsl:value-of select="concat($month, '/', $day, '/', $year)" />
</xsl:template>
This would return the string 20131004 as 10/04/2013 which is correct.
What I need to do though is if the $month has a leading zero, to remove it. For example, 20130930 would be 09/30/2013 when I would prefer 9/30/2013.
What's the most efficient way to do that? I could do a choose/when before I set the value of the variable but I'm trying to do it in the proper manner with xslt (I'm still trying to get into it, it's coming along).
Thanks
You could utilize number() function
<xsl:variable name="month" select="number(substring($date, 5, 2))" />
<xsl:variable name="day" select="number(substring($date, 7, 2))" />
It should remove leading zero.