mysql - 使用 XSLT 将 RSS pubDate 转换为 mySQL 时间戳格式

标签 mysql xslt rss

我有一个小工具,可以删除并重新排列 iTunes 格式的 RSS 提要,并将其转换为一个漂亮的简单 XML 文件。

然后,我将清理后的 XML 导入到 mySQL 中以供稍后使用。

我需要能够将 feed 中的 pubDate 转换为 mySQL 时间戳,以便我可以将其正确导入到表中的 TIMESTAMP 字段中。

我遇到了一些问题。

我当前的 XSL 文件对日期进行了整理,但我根本不需要这个。

我只想要<pubDate>节点内部具有正确的 mySQL 友好时间戳。

我还没有找到任何可以满足我需要的东西。有什么指点吗?

这是我的 XSLT 文件...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:atom="http://www.w3.org/2005/Atom" 
    xmlns:cc="http://web.resource.org/cc/"
    xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
    xmlns:libsyn="http://libsyn.com/rss-extension" 
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    exclude-result-prefixes="atom cc itunes libsyn media rdf">

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <data>
            <entries>
                <xsl:apply-templates select="rss/channel/item"></xsl:apply-templates>
            </entries>
        </data>
    </xsl:template>

    <xsl:template match="item">
        <entry>
            <title><xsl:value-of select="title"/></title>
            <link><xsl:value-of select="link"/></link>
            <description><xsl:value-of select="description" disable-output-escaping="yes"/></description>
            <subtitle><xsl:value-of select="itunes:subtitle"/></subtitle>
            <pubDate><xsl:value-of select="pubDate"/></pubDate>
            <xsl:apply-templates select="pubDate"/>
            <explicit><xsl:value-of select="itunes:explicit"/></explicit>
            <podcastImage><xsl:value-of select="itunes:image/@href"/></podcastImage>
            <podcastURL><xsl:value-of select="enclosure/@url"/></podcastURL>
            <podcastLength><xsl:value-of select="enclosure/@length"/></podcastLength>
            <podcastDuration><xsl:value-of select="itunes:duration"/></podcastDuration>
        </entry>
    </xsl:template>

    <xsl:template match="pubDate">
        <date>
            <xsl:attribute name="time"><xsl:value-of select="substring(text(),18,5)"/></xsl:attribute>
            <xsl:call-template name="format-from-rfc-to-iso">
                <xsl:with-param name="rfc-date" select="text()"/>
            </xsl:call-template>
        </date>
    </xsl:template>

    <xsl:template name="format-from-rfc-to-iso">
        <xsl:param name="rfc-date"/>
        <xsl:param name="day-with-zero" select="format-number(substring(substring($rfc-date,6,11),1,2),'00')"/>
        <xsl:param name="month-with-zero">
            <xsl:if test="contains($rfc-date,'Jan')">01</xsl:if>
            <xsl:if test="contains($rfc-date,'Feb')">02</xsl:if>
            <xsl:if test="contains($rfc-date,'Mar')">03</xsl:if>
            <xsl:if test="contains($rfc-date,'Apr')">04</xsl:if>
            <xsl:if test="contains($rfc-date,'May')">05</xsl:if>
            <xsl:if test="contains($rfc-date,'Jun')">06</xsl:if>
            <xsl:if test="contains($rfc-date,'Jul')">07</xsl:if>
            <xsl:if test="contains($rfc-date,'Aug')">08</xsl:if>
            <xsl:if test="contains($rfc-date,'Sep')">09</xsl:if>
            <xsl:if test="contains($rfc-date,'Oct')">10</xsl:if>
            <xsl:if test="contains($rfc-date,'Nov')">11</xsl:if>
            <xsl:if test="contains($rfc-date,'Dec')">12</xsl:if>
        </xsl:param>
        <xsl:param name="year-full" select="format-number(substring(substring($rfc-date,6,11),7,5),'####')"/>
        <xsl:param name="rfc-date-to-iso" select="concat($year-full,'-',$month-with-zero,'-',$day-with-zero)"/>

        <xsl:value-of select="$rfc-date-to-iso"/>

    </xsl:template>

</xsl:stylesheet>

当前日期/时间在 rss feed 中看起来像这样:

<pubDate>Sun, 07 Feb 2016 00:00:56 -0500</pubDate>

我希望它像这样显示,以便可以将其插入到 mySQL 中:

<pubDate>2016-02-07 00:00:56</pubDate>

我使用 PHP 来处理这个。

$xml = new DOMDocument;
$xml->load('podbean.xml');

$xsl = new DOMDocument;
$xsl->load('podbean.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

$proc->transformToXML($xml);

$proc->transformToURI($xml,'itunes.xml');

西蒙

最佳答案

使用支持 EXSLT str:tokenize() 函数(如 libxslt 所做的那样)的处理器,您可以执行以下操作:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- ... -->

<xsl:template match="item">
    <entry>
        <!-- ... -->
        <pubDate>
            <xsl:variable name="date-tokens" select="str:tokenize(pubDate, ' ' )" />
            <!-- year -->
            <xsl:value-of select="$date-tokens[4]" />
            <xsl:text>-</xsl:text>
            <!-- month -->
            <xsl:variable name="mmm" select="$date-tokens[3]" />
            <xsl:variable name="m" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" />
            <xsl:value-of select="format-number($m, '00')" />
            <xsl:text>-</xsl:text>  
            <!-- day -->
            <xsl:value-of select="format-number($date-tokens[2], '00')" />
            <xsl:text> </xsl:text>
            <!-- time -->
            <xsl:value-of select="$date-tokens[5]" />
        </pubDate>
        <!-- ... -->
    </entry>
</xsl:template>

</xsl:stylesheet>

关于mysql - 使用 XSLT 将 RSS pubDate 转换为 mySQL 时间戳格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35360515/

相关文章:

php - 使用 php 生成数组的 PDO 中的 SELECT 语句

mysql - Autowiring 时sqlSession为空(mybatis3,Spring5)

php - 如何替换链接子项以创建适合播客的提要?

android - 在 Webview Activity 中打开 RSS 链接

xml - 使用 XSL 计算 XML 中的端点

php - 使用 PHP 从标记名中有冒号的 RSS 提要获取附件标记

mysql - 表单选择存储 ID 而不是 NAME

java - 通过 PHP 从 java 到 mySQL 的安全三层连接

xml - 自动反转 xsl

java - 无法打开 ServletContext 资源 [/templates/show.xsl]