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
Related
I have these two nodes in XML:
<firstregistrationdate>01.10.2016.</firstregistrationdate>
<currentdate>14.10.2021.</currentdate>
And I would like to calculate days or years between them so if anybody has an idea how to do that it would be very helpful.
Thanks!
If you only want the difference between the two years, you could do simply:
<xsl:variable name="startyear" select="substring(firstregistrationdate, 7, 4)"/>
<xsl:variable name="endyear" select="substring(currentdate, 7, 4)"/>
<result>
<xsl:value-of select="xs:integer($endyear) - xs:integer($startyear)"/>
</result>
Demo: https://xsltfiddle.liberty-development.net/nbsuwEG
Added:
Calculating the difference in days between two dates is also fairly trivial in XSLT 2.0. The complication in your case is that the input dates are not in the expected YYYY-MM-DD format, so it is necessary to convert them first:
<xsl:variable name="startdate" select="replace(firstregistrationdate, '(.{2})\.(.{2})\.(.{4})\.', '$3-$2-$1')"/>
<xsl:variable name="enddate" select="replace(currentdate, '(.{2})\.(.{2})\.(.{4})\.', '$3-$2-$1')"/>
<result>
<xsl:value-of select="days-from-duration(xs:date($enddate)-xs:date($startdate))"/>
</result>
Demo: https://xsltfiddle.liberty-development.net/nbsuwEG/1
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>
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>
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.
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>