Adnaan Ali
Adnaan Ali

Reputation: 3

XSLT convert current date to iso_8601

I was wondering if it was possible to convert a current date format to an ISO_8601 format using XSLT.

In the XML the date currently is set to:

<end_date>
<![CDATA[ 2015-10-14 23:59:59 ]]>

Upvotes: 0

Views: 1042

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117140

In your example,

<xsl:value-of select="translate(normalize-space(end_date), ' ', 'T')"/>

will return:

2015-10-14T23:59:59

This is a valid ISO 8601 representation of date and local time.

If you are sure that the given value is in UTC (although I don't see any such indication in your input) , and you want to indicate this in the result, you could do:

<xsl:value-of select="concat(translate(normalize-space(end_date), ' ', 'T'), 'Z')"/>

to return:

2015-10-14T23:59:59Z

Upvotes: 1

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

Here is a XSLT that does the work :

  <?xml version="1.0" encoding="utf-8" ?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:i="http://schemas.datacontract.org/2004/07/Uanet>"
                      >
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="end_date">
      <!--copy current node-->
      <xsl:copy>
        <xsl:apply-templates select="text()" />
      </xsl:copy> 
    </xsl:template>

    <!--
    input : 2015-10-14 23:59:59 
    output : 2015-10-14T23:59:59Z 
    -->
    <xsl:template match="text()">
      <xsl:variable name="trimmed" select="." />
      <xsl:value-of select="substring($trimmed,0,12)" />
      <xsl:text>T</xsl:text>
      <xsl:value-of select="substring($trimmed,13,8)" />
      <xsl:text>Z</xsl:text>
    </xsl:template>

  </xsl:stylesheet>

Upvotes: 0

Related Questions