XSLT : compare date timezone - xslt

i am new to XSLT
How to compare the date strings in XSLT and output the result
below is my input xml
<?xml version="1.0" encoding="UTF-8"?>
<dates>
<date1>2003-09-15T16:53:22.000-07:00</date1>
<date2>2003-09-15T16:53:23.000-07:00</date2>
</dates>
below is my XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="date1" select="/dates/date1"/>
<xsl:variable name="date2" select="/dates/date2"/>
<xsl:template match="/">
<xsl:if test="$date1 > $date2">
date1 is greater
</xsl:if>
<xsl:if test="$date1 = $date2">
both dates are equal
</xsl:if>
<xsl:if test="$date1 <= $date2">
date1 is lesser than date2
</xsl:if>
</xsl:template>
</xsl:stylesheet>
now in XSLT i want to compare the above dates, so is it possible in XSLT 1.0 to compare (greater,lesser, equals) the dates
i believe in xslt 1.0 its not possible, if possible please share the information.
if it can be done in XSLT 2.0 please help me how i can fix this,
in my current ongoing study project, i have used XSLT 1.0, so please suggest answer in XSLT 1.0 thanks

To do this in XSLT 1.0, you must first convert the given dateTimes to numerical values and equalize them to a common time zone.
In the following stylesheet, each dateTime is converted to the number of seconds elapsed since noon UTC of November 24, 4714 BC - see: https://en.wikipedia.org/wiki/Julian_day
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="dates">
<xsl:variable name="date1">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="date1" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="date2">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="date2" />
</xsl:call-template>
</xsl:variable>
<result>
<xsl:choose>
<xsl:when test="$date1 < $date2">date1 occurs earlier than date2</xsl:when>
<xsl:when test="$date1 = $date2">the two dates are concurrent</xsl:when>
<xsl:when test="$date1 > $date2">date1 occurs later than date2</xsl:when>
</xsl:choose>
</result>
</xsl:template>
<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:variable name="date" select="substring-before($dateTime, 'T')" />
<xsl:variable name="time" select="substring-after($dateTime, 'T')" />
<xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 6)" />
<xsl:variable name="offset" select="substring-after($time, $local-time)" />
<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="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />
<xsl:variable name="offset-sign" select="1 - 2 * starts-with($offset, '-')" />
<xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign" />
<xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign" />
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>
<xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600*$offset-hour - 60*$offset-minute" />
</xsl:template>
</xsl:stylesheet>
Applied to the following test input:
XML
<dates>
<date1>2003-09-15T16:53:22.000-07:00</date1>
<date2>2003-09-15T17:53:22.000-06:00</date2>
</dates>
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<result>the two dates are concurrent</result>

Related

XSLT 1.0 Date Duration (Years)

I'm using XSLT 1.0 to output the duration between two date fields. The following code (from https://stackoverflow.com/a/38615456/3016153) outputs the Duration in Days, Hours, Minutes, and Seconds as expected, but I need to update it to output Years as well. Does anyone have any suggestions on how I can update this code to output the Year duration? Alternative code is welcomed as well, but it must use XSLT 1.0
Any assistance is greatly appreciated.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="event">
<xsl:variable name="start">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="start/#time" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="end">
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="end/#time" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="duration" select="$end - $start" />
<xsl:variable name="d" select="floor($duration div 86400)"/>
<xsl:variable name="t" select="$duration mod 86400"/>
<xsl:variable name="h" select="floor($t div 3600)"/>
<xsl:variable name="r" select="$t mod 3600"/>
<xsl:variable name="m" select="floor($r div 60)"/>
<xsl:variable name="s" select="$r mod 60"/>
<xsl:copy>
<xsl:copy-of select="name"/>
<duration>
<xsl:value-of select="$d"/>
<xsl:text> days, </xsl:text>
<xsl:value-of select="$h"/>
<xsl:text> hours, </xsl:text>
<xsl:value-of select="$m"/>
<xsl:text> minutes and </xsl:text>
<xsl:value-of select="$s"/>
<xsl:text> seconds</xsl:text>
</duration>
</xsl:copy>
</xsl:template>
<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:variable name="date" select="substring-before($dateTime, 'T')" />
<xsl:variable name="time" select="substring-after($dateTime, 'T')" />
<xsl:variable name="local-time" select="substring($time, 1, string-length($time) - 6)" />
<xsl:variable name="offset" select="substring-after($time, $local-time)" />
<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="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />
<xsl:variable name="offset-sign" select="1 - 2 * starts-with($offset, '-')" />
<xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign" />
<xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign" />
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>
<xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600*$offset-hour - 60*$offset-minute" />
</xsl:template>
</xsl:stylesheet>
Sample XML:
<events>
<event>
<name>Alpha</name>
<start time="2015-02-21T00:59:06+02:00"/>
<end time="2018-02-22T02:24:38+02:00"/>
</event>
<event>
<name>Bravo</name>
<start time="2016-02-21T00:59:06+02:00"/>
<end time="2020-03-05T02:24:38+02:00"/>
</event>
<event>
<name>Charlie</name>
<start time="2016-02-21T00:59:06+02:00"/>
<end time="2020-02-21T00:59:06+01:00"/>
</event>
enter code here
As stated in the comments, if you are willing to assume that every 365 days constitute a year, then all you need to do is change this part:
<duration>
<xsl:value-of select="$d"/>
<xsl:text> days, </xsl:text>
<xsl:value-of select="$h"/>
<xsl:text> hours, </xsl:text>
<xsl:value-of select="$m"/>
<xsl:text> minutes and </xsl:text>
<xsl:value-of select="$s"/>
<xsl:text> seconds</xsl:text>
</duration>
to:
<duration>
<xsl:value-of select="floor($d div 365)"/>
<xsl:text> years, </xsl:text>
<xsl:value-of select="$d mod 365"/>
<xsl:text> days, </xsl:text>
<xsl:value-of select="$h"/>
<xsl:text> hours, </xsl:text>
<xsl:value-of select="$m"/>
<xsl:text> minutes and </xsl:text>
<xsl:value-of select="$s"/>
<xsl:text> seconds</xsl:text>
</duration>
As also stated in the comments, it is rather rather strange that you would be willing to accept an error of approximately 1 day per 4 years in a calculation that calculates to seconds precision. If you only need precision of days, then you should save processor time and electricity by removing the parts that calculate the hours, minutes and seconds.

how to know week day using xslt 1.0 [duplicate]

Trying to calculate day of the week number from date. Found some examples in the internet, but instead of day of the week number i see this : NaN.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="date">
<xsl:copy>
<xsl:call-template name="date-format">
<xsl:with-param name="date-time" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="date-format">
<xsl:param name="date-time"/>
<xsl:param name="date" select="substring-before($date-time,'T')"/>
<xsl:param name="year" select="substring-before($date,'-')"/>
<xsl:param name="month"
select="substring-before(substring-after($date,'-'),'-')"/>
<xsl:param name="day" select="substring-after(substring-after($date,'-'),'-')"/>
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year - $a"/>
<xsl:variable name="m" select="$month + 12 * $a - 2"/>
<xsl:value-of select="($day + $y + floor($y div 4) - floor($y div 100)
+ floor($y div 400) + floor((31 * $m) div 12)) mod 7"/>
</xsl:template>
</xsl:stylesheet>
.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test2.xsl" type="text/xsl" ?>
<document>
<date>2013-01-01</date>
<date>2013-05-24</date>
<date>2013-12-25</date>
<date>1957-07-13</date>
<date>1776-07-04</date>
</document>
The following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="date">
<xsl:call-template name="day-of-week">
<xsl:with-param name="date" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="day-of-week">
<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="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>
<xsl:variable name="JDN" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="($JDN + 1) mod 7" />
</xsl:template>
</xsl:stylesheet>
when applied to your input example:
<document>
<date>2013-01-01</date>
<date>2013-05-24</date>
<date>2013-12-25</date>
<date>1957-07-13</date>
<date>1776-07-04</date>
</document>
will return the following result:
<?xml version="1.0" encoding="UTF-8"?>
25364
Note:
The main problem with your attempt is this:
<xsl:param name="date" select="substring-before($date-time,'T')"/>
Your input has dates, not date-times, and they do not contain "T".

xsl: Sort Multiple attributes

I have the below data, where I have multiple attributes:
I need to sort either by Datetime or by Timezone in ascending order.
If input contains different Datetime and same Timezone it should output ascending per Datetime.
If input contains same Datetime and different Timezone it should output ascending per Timezone.
Current Logic:
<xsl:sort select="#Datetime" order="ascending"/>
<xsl:sort select="#Timezone" order="ascending"/>
SAMPLE 1st scenario - output is as expected:
<MSG>
<DOC>
<Parent>
<Input Id="1234567890" Srvce="RRR" Cd="D1" Datetime="2016-06-16 20:42:30" Timezone="+02:00" EvtRmk="DUMMY 1">
</Input>
<Input Id="1234567890" Srvce="RRR" Cd="D1" Datetime="2016-06-15 20:43:15" Timezone="+04:00" EvtRmk="DUMMY 1">
</Input>
Current output:
<Output>2016-06-15 20:43:15"</Output>
<Output>2016-06-16 20:42:30</Output>
SAMPLE 2nd scenario - Different Timezone/Same Datetime; output not sorted per Timezone:
<MSG>
</DOC>
<Parent>
<Input Id="1234567890" Srvce="RRR" Cd="D1" Datetime="2016-06-15 20:42:30" Timezone="+04:00" EvtRmk="DUMMY 1">
</Input>
<Input Id="1234567890" Srvce="RRR" Cd="D1" Datetime="2016-06-15 20:43:15" Timezone="+00:00" EvtRmk="DUMMY 1">
</Input>
Current Output:
<Output>2016-06-15 20:42:30</Output>
<Output>2016-06-15 20:43:15</Output>
Expected output:
<Output>2016-06-15 20:43:15</Output>
<Output>2016-06-15 20:42:30</Output>
In XSLT 1.0, you will have to do this in two passes: first, equalize the input dateTimes to a common base such as UTC, then sort the results by this value:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Parent">
<!-- first-pass -->
<xsl:variable name="inputs">
<xsl:for-each select="Input">
<input>
<xsl:copy-of select="#*"/>
<xsl:call-template name="dateTime-to-seconds">
<xsl:with-param name="dateTime" select="#Datetime" />
<xsl:with-param name="offset" select="#Timezone" />
</xsl:call-template>
</input>
</xsl:for-each>
</xsl:variable>
<!-- output -->
<xsl:copy>
<xsl:for-each select="exsl:node-set($inputs)/input">
<xsl:sort select="." data-type="number" order="ascending"/>
<Input>
<xsl:copy-of select="#*"/>
</Input>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template name="dateTime-to-seconds">
<xsl:param name="dateTime"/>
<xsl:param name="offset"/>
<xsl:variable name="date" select="substring-before($dateTime, ' ')" />
<xsl:variable name="local-time" select="substring-after($dateTime, ' ')" />
<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="hour" select="substring($local-time, 1, 2)" />
<xsl:variable name="minute" select="substring($local-time, 4, 2)" />
<xsl:variable name="second" select="substring($local-time, 7)" />
<xsl:variable name="offset-sign" select="1 - 2 * starts-with($offset, '-')" />
<xsl:variable name="offset-hour" select="substring($offset, 2, 2) * $offset-sign" />
<xsl:variable name="offset-minute" select="substring($offset, 5, 2) * $offset-sign" />
<xsl:variable name="a" select="floor((14 - $month) div 12)"/>
<xsl:variable name="y" select="$year + 4800 - $a"/>
<xsl:variable name="m" select="$month + 12*$a - 3"/>
<xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
<xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second - 3600*$offset-hour - 60*$offset-minute" />
</xsl:template>
</xsl:stylesheet>
Applied to the following test input:
XML
<Parent>
<Input Id="001" Datetime="2016-06-15 19:44:30" Timezone="-02:30"/>
<Input Id="002" Datetime="2016-06-15 20:42:30" Timezone="+04:00"/>
<Input Id="003" Datetime="2016-06-15 21:43:15" Timezone="+00:00"/>
</Parent>
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<Parent>
<Input Id="002" Datetime="2016-06-15 20:42:30" Timezone="+04:00"/>
<Input Id="003" Datetime="2016-06-15 21:43:15" Timezone="+00:00"/>
<Input Id="001" Datetime="2016-06-15 19:44:30" Timezone="-02:30"/>
</Parent>

XSL to capture URL Params

Not able to extract the params using XSLT,
for eg.: http://www.example.com/AB/100/123456/09/8
using XSLT need to extract like var1=AB, Var2=100, Var3=123456, var4=09, var5=8, All the params are mandatory. and var3 can accept 1-999999, could somebody share some ideas
tried Substring but it didn't help much
Though the input is not clear, just as example: given an input XML
<root>
<url>http://www.example.com/AB/100/123456/09/8</url>
</root>
following XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="url">
<xsl:call-template name="parse">
<xsl:with-param name="url" select="substring-after(.,'//')" />
<xsl:with-param name="position" select="1" />
</xsl:call-template>
</xsl:template>
<xsl:template name="parse">
<xsl:param name="url" />
<xsl:param name="position" />
<xsl:choose>
<xsl:when test="contains(substring-after($url, '/'),'/')">
<xsl:value-of select="concat('Var', $position, ': ')" />
<xsl:value-of select="substring-before(substring-after($url, '/'),'/')" />
<xsl:text>, </xsl:text>
<xsl:call-template name="parse">
<xsl:with-param name="url" select="substring-after($url, '/')" />
<xsl:with-param name="position" select="$position + 1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('Var', $position, ': ')" />
<xsl:value-of select="substring-after($url,'/')" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:transform>
produces the ouput
Var1: AB, Var2: 100, Var3: 123456, Var4: 09, Var5: 8
using the template parse which recursively calls itself when the url contains a /.
For every call the parameter url is reduced using substring-after() and the parameter position is incremented.

XSLT - Looping through an array of dates

I need to know all weekends from two dates. I actually got a template which returns the day number (0-6) when passing a date as parameter, however, the problem is i need to loop through the dates between the two dates and calculate just business days.
Is there a way to store all these dates in an array and loop through that array?
Do i need to use C# embedded?
I'm using XSLT V 1.0 with SharePoint Designer.
Thanks.
This is not exactly trivial to do in pure XSLT 1.0.
Weekend dates in a given range:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="startDate" select="'2014-05-01'"/>
<xsl:param name="endDate" select="'2014-05-31'"/>
<xsl:template match="/">
<weekends>
<xsl:call-template name="weekends">
<xsl:with-param name="startJDN">
<xsl:call-template name="JDN">
<xsl:with-param name="date" select="$startDate" />
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="endJDN">
<xsl:call-template name="JDN">
<xsl:with-param name="date" select="$endDate" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</weekends>
</xsl:template>
<xsl:template name="weekends">
<xsl:param name="startJDN"/>
<xsl:param name="endJDN"/>
<xsl:if test="$startJDN mod 7 > 4">
<date>
<xsl:call-template name="GD">
<xsl:with-param name="JDN" select="$startJDN" />
</xsl:call-template>
</date>
</xsl:if>
<xsl:if test="$startJDN < $endJDN">
<xsl:call-template name="weekends">
<xsl:with-param name="startJDN" select="$startJDN + 1"/>
<xsl:with-param name="endJDN" select="$endJDN"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="JDN">
<xsl:param name="date"/>
<xsl:param name="year" select="substring($date, 1, 4)"/>
<xsl:param name="month" select="substring($date, 6, 2)"/>
<xsl:param name="day" select="substring($date, 9, 2)"/>
<xsl:param name="a" select="floor((14 - $month) div 12)"/>
<xsl:param name="y" select="$year + 4800 - $a"/>
<xsl:param name="m" select="$month + 12*$a - 3"/>
<xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template>
<xsl:template name="GD">
<xsl:param name="JDN"/>
<xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
<xsl:param name="e" select="4*$f + 3"/>
<xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
<xsl:param name="h" select="5*$g + 2"/>
<xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
<xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
<xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
<xsl:value-of select="concat($Y, '-', format-number($M, '00'), '-', format-number($D, '00'))" />
</xsl:template>
</xsl:stylesheet>
The result of the above is a list of all Saturdays and Sundays in the month of May 2014:
<?xml version="1.0" encoding="UTF-8"?>
<weekends>
<date>2014-05-03</date>
<date>2014-05-04</date>
<date>2014-05-10</date>
<date>2014-05-11</date>
<date>2014-05-17</date>
<date>2014-05-18</date>
<date>2014-05-24</date>
<date>2014-05-25</date>
<date>2014-05-31</date>
</weekends>