如何在XSLT中将RSS XML中的特定日期字符串转换为指定格式
Solution to Transform RSS Date Format with XSLT
Got it, here's a solid XSLT 1.0 solution to convert your RSS date strings from Wednesday, April 25, 2018 - 11:00 to 2018-04-25 11:00 AM—no modifications needed to your source XML. This works with standard RSS 2.0 feeds and is compatible with most XML processors:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <!-- Identity template: copies all content unchanged except targeted date elements --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Match either pubDate or dc:date (adjust based on your feed's actual date element) --> <xsl:template match="pubDate|dc:date"> <xsl:copy> <xsl:call-template name="format-custom-date"> <xsl:with-param name="input-string" select="text()"/> </xsl:call-template> </xsl:copy> </xsl:template> <!-- Named template to handle the date transformation logic --> <xsl:template name="format-custom-date"> <xsl:param name="input-string"/> <!-- Extract the core date part (strip day name and time segment) --> <xsl:variable name="date-segment" select="substring-after($input-string, ', ')"/> <xsl:variable name="date-only" select="substring-before($date-segment, ' - ')"/> <!-- Split date into month name, day, and year --> <xsl:variable name="month-name" select="substring-before($date-only, ' ')"/> <xsl:variable name="day-year-part" select="substring-after($date-only, ' ')"/> <xsl:variable name="day" select="substring-before($day-year-part, ', ')"/> <xsl:variable name="year" select="substring-after($day-year-part, ', ')"/> <!-- Map full month name to two-digit number --> <xsl:variable name="month-number"> <xsl:choose> <xsl:when test="$month-name = 'January'">01</xsl:when> <xsl:when test="$month-name = 'February'">02</xsl:when> <xsl:when test="$month-name = 'March'">03</xsl:when> <xsl:when test="$month-name = 'April'">04</xsl:when> <xsl:when test="$month-name = 'May'">05</xsl:when> <xsl:when test="$month-name = 'June'">06</xsl:when> <xsl:when test="$month-name = 'July'">07</xsl:when> <xsl:when test="$month-name = 'August'">08</xsl:when> <xsl:when test="$month-name = 'September'">09</xsl:when> <xsl:when test="$month-name = 'October'">10</xsl:when> <xsl:when test="$month-name = 'November'">11</xsl:when> <xsl:when test="$month-name = 'December'">12</xsl:when> </xsl:choose> </xsl:variable> <!-- Process time and add AM/PM --> <xsl:variable name="time-segment" select="substring-after($input-string, ' - ')"/> <xsl:variable name="hour" select="substring-before($time-segment, ':')"/> <xsl:variable name="minute" select="substring-after($time-segment, ':')"/> <xsl:variable name="ampm"> <xsl:choose> <xsl:when test="$hour >= 12">PM</xsl:when> <xsl:otherwise>AM</xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Convert 24-hour to 12-hour format --> <xsl:variable name="formatted-hour"> <xsl:choose> <xsl:when test="$hour = 0">12</xsl:when> <xsl:when test="$hour > 12"><xsl:value-of select="$hour - 12"/></xsl:when> <xsl:otherwise><xsl:value-of select="$hour"/></xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Assemble the final formatted date --> <xsl:value-of select="concat($year, '-', $month-number, '-', $day, ' ', $formatted-hour, ':', $minute, ' ', $ampm)"/> </xsl:template> </xsl:stylesheet>
Key Details & Adjustments:
- Identity Template: This is critical—it copies every part of your RSS feed except the date elements you specify, so all other content stays intact.
- Date Element Match: The template is set to match both
<pubDate>and<dc:date>(common in RSS 2.0 feeds). If your feed uses only one, you can remove the unused option from thematchattribute. - Time Handling: Automatically converts 24-hour time to 12-hour format (e.g.,
13:30becomes1:30 PM,00:15becomes12:15 AM). - Month Mapping: The
<xsl:choose>block ensures full month names are converted to two-digit numbers (e.g.,April→04) to meet theYYYY-MM-DDdate standard.
How to Use:
- Save this XSLT as a
.xslfile. - Process your RSS XML with this stylesheet using an XSLT processor (like Saxon, or built-in tools in XML editors like Oxygen XML).
内容的提问来源于stack exchange,提问作者Rohit Gupta
相关产品推荐
相关产品推荐

