I have this in xml and I want to transform the document using xslt. How do I convert this dateServed string to date? <NotificationEvent dateServed="06/20/2014"</NotificationEvent>
I want the result to be 2014/06/20
You don't need to convert "06/20/2014" to a date in order to "format" it as "2014/06/20". You can simply re-arrange it using string functions:
<xsl:value-of select="concat(substring(#dateServed, 7), '/', substring(#dateServed, 1, 5))"/>
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. :-)
i have troubles filtering with XSLT, basically i have an xml, where i would like to get only those items where the Due Date falls into interval of 3 months ... i've written the following:
<xsl:variable name="TaskRows" select="/dsQueryResponse/Tasks/Rows/Row[#DueDate > $IsoBeginQuartDate AND #DueDate < $IsoEndQuartDate]"/>
But I get an error: "Failed seting processor stylesheet: expected token ']' found 'NAME' ...
IsoDates are calculated and formated dates in ISO format ...
Any idea, how to do it, or i can't use "AND" when filtering?
PS: i'm using XSLT 1.0
There is case-sensitivity at work here! Try using 'and' instead of 'AND'.
Unfortunately in XSLT 1.0, you can't compare dates to see if a date is between two values.
What you could do is something like this:
<!-- Make sure to format this variables as YYYYMMDD -->
<xsl:variable name="$IsoBeginQuartDate">20130101</xsl:variable>
<xsl:variable name="$IsoBeginQuartDate">20131231</xsl:variable>
<!-- Make sure #DueDate also has the format YYYYMMDD, in this example I assume the DueDate has format YYYY-MM-DD -->
<xsl:variable name="TaskRows" select="/dsQueryResponse/Tasks/Rows/Row[translate(#DueDate, '-', '') > $IsoBeginQuartDate and translate(#DueDate, '-', '') < $IsoEndQuartDate]"/>
You now get errors, because > and < or not supported for strings or dates.
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]')" />
Hope someone can help. I am trying to compare 2 dates inside an XML file and using a XSLT to do some calculation:
For example I have 2 dates in XML: 2011-05-23 and 2011-04-29. I want to do calculation inside XSLT like below:
('2011-05-23'-'2011-04-29')*30 = 24*30= 720
Can anyone shed any light?
An XSLT 2.0 solution
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select="days-from-duration(
xs:date('2011-05-23')
- xs:date(xs:date('2011-04-29'))
)*30"/>
</xsl:template>
</xsl:stylesheet>
Yields: 720
xs:date() function evaluates the dates, which can be used to perform date operations
subtracting the second date from the first yields the xdt:dayTimeDuration P24D (24 days)
days-from-duration() extracts the days component from the xdt:dayTimeDuration (24)
then you can use that number to perform normal arethmatic (e.g. 24*30=720)
It might be worth looking at the EXSLT - date:difference solution. I believe that should do what you want, and there's even a straight XSLT implementation available.
Be aware though that the returned value is in duration format as specified in the XML Schema Part 2: Datatypes Second Edition, so it's likely you will need to process the result to derive the unit you wish to use in calculation (for instance in your example above you're expecting a result detailing the number of days difference - so you would need to pull out the relevant unit you want to work with, the result from date:difference in that case would most likely be "P24D").
Here's two templates I sometimes use for date calculations:
<xsl:template name="calcseconds">
<xsl:param name="date" />
<xsl:value-of select="(((substring($date,1,4) - 1970) * 365)+floor((substring($date,1,4) - 1970) div 4)+substring('000,031,059,090,120,151,181,212,243,273,304,334,365',substring($date,6,2)*4-3,3)+(substring($date,9,2)-1)+(1-floor(((substring($date,1,4) mod 4) + 2) div 3))*floor((substring($date,6,2)+17) div 20))*86400+(substring($date,12,2)*3600)+(substring($date,15,2)*60)+substring($date,18,2)" />
</xsl:template>
<xsl:template name="calcdays">
<xsl:param name="date" />
<xsl:value-of select="(((substring($date,1,4) - 1970) * 365)+floor((substring($date,1,4) - 1970) div 4)+substring('000,031,059,090,120,151,181,212,243,273,304,334,365',substring($date,6,2)*4-3,3)+(substring($date,9,2)-1)+(1-floor(((substring($date,1,4) mod 4) + 2) div 3))*floor((substring($date,6,2)+17) div 20))" />
</xsl:template>
They're a bit of a mouthful, but they'll calculate the number of seconds/days since midnight 1st January 1970 as an integer, which you can then do straight arithmetic on. They rely on the date format being yyyy-mm-dd hh:mm:ss, but manipulation of the parameters of the substring calls should allow you to process dates in any format you need.