This project is read-only.

parse date time in xslt

Jan 21, 2015 at 9:16 PM
I'm having trouble parsing a datetime out of our logging xml. The datetime includes the timezone offset, i don't want to include this offset, just treat the date as local. I've been using
<xsl:attribute name='d'>
     <xsl:value-of select="substring(@EventTimeStamp, 1, 23)"/>
</xsl:attribute>
But sometimes the milliseconds only includes 2 places so the 23 fails. I want to include milliseconds. Can you help in parsing the datetime out without the timezone offset.

thanks
scott

Here is a sample of our xml
<xLoggerDebug TransKey="[PID 16976]" Level="4096" Computer="RCSOGA03SAB" User="sbrogden" Application="MediaServerCaller.exe" Category="" Location="TRANSPORT" EventTimeStamp="2015-01-21T13:59:53.07-07:00">
  <Data>Recievetiming - Picked up msgIds: Guid: f1322dc2-1111-be91-4000-03d247b38d90 Count: 0</Data>
</xLoggerDebug>
<xLoggerDebug TransKey="[PID 16976]" Level="4096" Computer="RCSOGA03SAB" User="sbrogden" Application="MediaServerCaller.exe" Category="" Location="TRANSPORT" EventTimeStamp="2015-01-21T13:59:53.075001-07:00">
  <Data>Recievetiming - Received MessageHandle Set Agent: Guid: f1322dc2-1111-be91-4000-03d247b38d90</Data>
</xLoggerDebug>
Jan 22, 2015 at 6:42 PM
There is probably a way to solve this in the xslt but i modified the code to remove time zones. Thought this was more efficient than trying to parse and find within a string in xslt. Not sure if this works on a big picture but for my needs it works, works if the timezone is set and when it's not.

So now my xslt is
<xsl:attribute name='d'>
     <xsl:value-of select="@EventTimeStamp"/>
</xsl:attribute>
old
public static MessageTimestamp ParseFromLoselessFormat(string str)
{
    return new MessageTimestamp(XmlConvert.ToDateTime(str, XmlDateTimeSerializationMode.RoundtripKind));
}
new
public static MessageTimestamp ParseFromLoselessFormat(string str)
{
    return new MessageTimestamp(XmlConvert.ToDateTimeOffset(str).DateTime);
}
thanks
scott
Jan 30, 2015 at 11:45 AM
Edited Jan 30, 2015 at 11:48 AM
Hi Scott

You propose to change a core function. It may break other functionality.

Proper solution is to parse EventTimeStamp according to your requirements within your format definistion file.
XSLT string functions suck. You can use LogJoint XSLT extension functions. However even with those I could not find a beautiful solution.

To resolve the issie I just introduced a new extension method MATCH(string, regex) that one can use to extract a part of input string that matches a regex pattern. The idea is that you take a part of EventTimeStamp without hardcoding the length and then parse this part as a datetime.

Instead of
<xsl:attribute name='d'>
     <xsl:value-of select="@EventTimeStamp"/>
</xsl:attribute>
use that
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lj="http://logjoint.codeplex.com/">
...
<xsl:attribute name='d'>
     <xsl:value-of select='lj:TO_DATETIME(lj:MATCH(@EventTimeStamp, "^[\d\-T\:]{19}\.\d*"), "yyyy-MM-ddTHH:mm:ss.FFFFFFF")'/>
</xsl:attribute>
I did a very quick test of the solution. Works for me. Let me know it works for you as well.

Sergey
Feb 19, 2015 at 6:51 PM
works great

thanks
scott