Solved

How to Use XSLT to convert Epoch time

Posted on 2010-09-13
12
3,113 Views
Last Modified: 2013-11-18
I'm new to XSLT; we're starting to use version 2.

In my 3rd party XML document, there are recurring <TIME> tags. The <TIME> can be under the <CUSTOMER>, the <ORDER>, the <BACKORDER> etc. The value of <TIME> is an epoch time value.

I would like to either:
a) transform all of the values of <TIME> to format mm-dd-yyyy  or
b) add another node, say <READABLETIME>, with the mm-dd-yyyy value, and place it after (or before) the <TIME>

Can someone please show me how to structure this? I would prefer not to have to know all of the possible paths to <TIME>.

Thanks.
0
Comment
Question by:AIBMass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33666640
Try this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
     version="2.0">
<xsl:template match="TIME">
    <xsl:copy-of select="."/>
    <READABLETIME>
        <xsl:value-of select="substring-before(string(xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', ., 'S'))), 'T')"></xsl:value-of>
    </READABLETIME>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 33666724
Note that by having a template for <TIME> you can easily organise your stylesheet in a way that it activates each time there is a <TIME> element. Just use apply-templates consequently

Note that I cut of the date part. You can easily construct the mm-dd-yyy format by using functions such as day-from-dateTime() etc

this XML
<TIME>1234567890</TIME>

gives me this result
<TIME>1234567890</TIME>
<READABLETIME>2009-02-13</READABLETIME>
0
 

Author Closing Comment

by:AIBMass
ID: 33671808
Yes, perfect, thanks.
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33671825
welcome
0
 

Author Comment

by:AIBMass
ID: 33685645

Cannot find the script or external object that implements prefix 'http://www.w3.org/2001/XMLSchema'
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33685753
using an XSLT1 processor instead of a xslt2 processor I am afraid
0
 

Author Comment

by:AIBMass
ID: 33694669
upgraded to XSLT2 processor.  Thanks.  

Above:
<xsl:value-of select="substring-before(string(xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration(concat('PT', ., 'S'))), 'T')"></xsl:value-of>

Causes:  
Invalid duration value 'PT1282679470737S'
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33694904
which xslt processor is that?

13 digits for seconds in epoch time seems a lot
0
 

Author Comment

by:AIBMass
ID: 33694992
Saxon - Home Edition

maybe they're miliseconds?  The xml file is sent to us.  The error message says,

'Invalid duration value 'PT1282679470737S' <non-numeric component>
0
 

Author Comment

by:AIBMass
ID: 33695067
Looks like we had bad data.  If we truncate the input, it works great.  
Thanks for the insight.  
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33695099
explanation, highest possible integer on a 32bit system is 2147483647
the code works for that number, and not for anything higher
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33695119
This tells me that
2038-01-19
would be an interesting day for epoch time and 32-bit computers
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question