将日期字符串(日,月日,年 - 时)格式从xml转换为日期格式xslt

时间:2018-04-26 17:38:23

标签: xml xslt

如何转换字符串日期" 2018年4月25日星期三 - 11:00"在我的xslt代码中从输入xml到2018-04-25 11:00 AM格式。

以下是来自RSS feed的输入xml,无法更改。

<?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>

这是我写的XSL

<?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> 

我得到的输出低于。请注意,日期将以字符串形式出现,导致我的Feed失败。如何转换字符串日期&#34; 2018年4月25日星期三 - 11:00&#34;到2018-04-25 11:00 AM格式

<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>

1 个答案:

答案 0 :(得分:0)

由于您似乎可以访问MSXSL命名空间,因此可以向XSL样式表添加脚本扩展,以更合适的编程语言执行日期转换(请参阅MSDN)。

为了示例,我们使用JScript,但也可以在脚本块中使用.NET语言(参见MSDN)。

以下使用<msxsl:script>块来定义convertDate()函数,该函数稍后将在带有前缀的XSLT代码中使用。它会将此格式的有效日期"Wednesday, April 25, 2018 - 11:00"转换为"2018-04-25 11:00"。任何它无法转换的东西,都会保持不变。

<msxsl:script>允许与您的<xsl:template>元素所在的级别相同。

<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>