【问题标题】:Convert date string coming as(Day,Month Day,Year -time) format from xml to date format in xslt将日期字符串作为(日,月日,年-时间)格式从 xml 转换为 xslt 中的日期格式
【发布时间】:2018-04-27 02:13:27
【问题描述】:

如何在我的 xslt 代码中将来自输入 xml 的字符串日期“2018 年 4 月 25 日星期三 - 11:00”转换为 2018-04-25 11:00 AM 格式。

以下是来自 RSS 提要且无法更改的输入 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> 

我得到的输出如下。请注意日期以字符串形式出现,这导致我的提要失败。如何将字符串日期“2018 年 4 月 25 日星期三 - 11:00”转换为 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>

【问题讨论】:

标签: xml xslt


【解决方案1】:

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

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

以下使用&lt;msxsl:script&gt; 块来定义convertDate() 函数,该函数稍后将在您的XSLT 代码中使用前缀。它将这种格式的有效日期"Wednesday, April 25, 2018 - 11:00" 转换为"2018-04-25 11:00"。任何它无法转换的东西,它都会原封不动地返回。

&lt;msxsl:script&gt; 允许在您的 &lt;xsl:template&gt; 元素所在的同一级别上。

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

【讨论】:

  • xslt 2.0 或 3.0 中是否有任何内置函数可以做到这一点?
  • 您在 XSLT 2.0+ 中具有与正则表达式和日期相关的函数,相关线程 stackoverflow.com/questions/1575111/…。您展示的样式表专门使用 XSLT 1.0,所以我假设您没有其他可用的。
  • XPath 3.1 有函数 parse-ietf-date()
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多