Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting dateand in XML to UTC timezone with XSLT

Tags:

xml

xslt

I have an XML document that has dates in standard ISO 8601 format. Like this:


2011-11-29T04:15:22-08:00

I would like to convert the time to UTC and the output date in the following form using XSLT:


2011-11-29 12:15:22

How can it be done?

Thanks in advance.

like image 954
Zaar Hai Avatar asked Jan 17 '23 22:01

Zaar Hai


1 Answers

The following XPath 2.0 expression produces the wanted string value:

  translate(
    string(
       adjust-dateTime-to-timezone(
          xs:dateTime('2011-11-29T04:15:22-08:00'),
          xs:dayTimeDuration('PT0H')
                              )
         ),
     'TZ',
     ' '
            )

XSLT - based verification:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:sequence select=
     "translate(
        string(
           adjust-dateTime-to-timezone(
              xs:dateTime('2011-11-29T04:15:22-08:00'),
              xs:dayTimeDuration('PT0H')
                                  )
             ),
         'TZ',
         ' '
                )
     "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on any XML document (not used), the XPath expression is evaluated and the result of this evaluation is output:

2011-11-29 12:15:22
like image 196
Dimitre Novatchev Avatar answered Jan 30 '23 23:01

Dimitre Novatchev