【发布时间】: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><p>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><p>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>
【问题讨论】:
-
也许this SO question 会激励你。