Zaar Hai
Zaar Hai

Reputation: 9859

Converting dateand in XML to UTC timezone with 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.

Upvotes: 4

Views: 14584

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

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

Upvotes: 9

Vitaliy
Vitaliy

Reputation: 2804

XSLT-1.0 doesn't have functions for date formatting.

Therefore you have to work with it as with text - see related question Format a date in XML via XSLT

Other possible way is to create extension functions (see @0xA3 answer in the same question in the case of MSXSL and .Net)

Upvotes: 0

Related Questions