Rohit Gupta
Rohit Gupta

Reputation: 41

Convert date string coming as(Day,Month Day,Year -time) format from xml to date format in xslt

How can I convert the string date "Wednesday, April 25, 2018 - 11:00" coming from input xml to 2018-04-25 11:00 AM format in my xslt code.

following is the input xml which is coming from RSS feed and cant be changed.

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://www.hhs.gov/rss/news.xml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Latest News Releases</title>
        <link>https://www.hhs.gov/rss/news.xml</link>
        <description>HHS News Releases</description>
        <language>en</language>
        <atom:link href="https://www.hhs.gov/rss/news.xml" rel="self" type="application/rss+xml" />
        <item>
            <title>Secretary Azar, Surgeon General Adams Praise Private Sector Support for Naloxone Advisory</title>
            <link>https://www.hhs.gov/about/news/2018/04/25/secretary-azar-surgeon-general-adams-praise-private-sector-support-naloxone-advisory.html</link>
            <description>&lt;p&gt;Following the early April release of the Surgeon General’s Advisory on Naloxone and Opioid Overdose
            </description>
            <pubDate>Wednesday, April 25, 2018 - 11:00</pubDate>
            <dc:creator>HHS Press Office</dc:creator>
            <guid isPermaLink="true">https://www.hhs.gov/about/news/2018/04/25/secretary-azar-surgeon-general-adams-praise-private-sector-support-naloxone-advisory.html</guid>
        </item>
    </channel>
</rss>

Here is the XSL I have written

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"

    exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/rss">
        <Records>
            <xsl:apply-templates select="channel/item" /> 
        </Records>

    </xsl:template>      

    <xsl:template match="channel/item">
        <xsl:element name="Record">
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>    

    <xsl:template match="*">        
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>  

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet> 

The output i am getting is below. Notice the date is coming as string which is causing my feed to fail. How can I convert the string date "Wednesday, April 25, 2018 - 11:00" to 2018-04-25 11:00 AM format

<Record>
            <title>Secretary Azar, Surgeon General Adams Praise Private Sector Support for Naloxone Advisory</title>
            <link>https://www.hhs.gov/about/news/2018/04/25/secretary-azar-surgeon-general-adams-praise-private-sector-support-naloxone-advisory.html</link>
            <description>&lt;p&gt;Following the early April release of the Surgeon General’s Advisory on Naloxone and Opioid Overdose
            </description>
            <pubDate>Wednesday, April 25, 2018 - 11:00</pubDate>
            <creator>HHS Press Office</creator>
            <guid isPermaLink="true">https://www.hhs.gov/about/news/2018/04/25/secretary-azar-surgeon-general-adams-praise-private-sector-support-naloxone-advisory.html</guid>
        </Record>

Upvotes: 0

Views: 445

Answers (1)

Tomalak
Tomalak

Reputation: 338396

Since you seem to have access to the MSXSL namespace, you can add a script extension to your XSL stylesheet that does the date conversion in a more suitable programming language (see MSDN).

For the sake of the example, let's use JScript, but using .NET languages in the script block is also possible (see MSDN).

The following uses an <msxsl:script> block to define a convertDate() function, which will later be usable in your XSLT code with a prefix. It converts valid dates in this format "Wednesday, April 25, 2018 - 11:00" to "2018-04-25 11:00". Anything it cannot convert, it returns unchanged.

<msxsl:script> is allowed on the same level where your <xsl:template> elements are.

<msxsl:script language="JScript" implements-prefix="script">
<![CDATA[

// converts dates like this one: "Wednesday, April 25, 2018 - 11:00"
// to "2018-04-25 11:00"
function convertDate(text) {
    var m = text.match(/[a-z]+, ([a-z]+) (\d{1,2}), (\d{4}) - (\d\d:\d\d)/i),
        zero = function (s) { return ('0' + s).slice(-2); },
        dateStr, date;

    if (m) {
        dateStr = [m[2], m[1], m[3], m[4]].join(' ');
        date = new Date(Date.parse(dateStr));
        if (date) {
            return [
                date.getFullYear(),
                '-',
                zero(date.getMonth() + 1),
                '-',
                zero(date.getDate()),
                ' ',
                zero(date.getHours()),
                ':',
                zero(date.getMinutes())
            ].join('');
        }
    }
    return text;
}

]]>
</msxsl:script>

<xsl:template match="pubDate">
    <xsl:copy>
        <xsl:value-of select="script:convertDate(.)"/>
    </xsl:copy>
</xsl:template>

Upvotes: 0

Related Questions