2016-02-12 145 views
0

我有一个小工具,它可以去掉并重新排列iTunes格式的RSS源并将其转换为一个简单的XML文件。使用XSLT将RSS pubDate转换为mySQL时间戳格式

然后我将清理后的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提要:

<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'); 

西蒙

+0

你的问题不明确。请告诉我们输入是什么样的,输出应该是什么样子。 –

+0

我刚刚编辑了这个问题,以包含来自RSS的当前输入和我需要的输出格式。 –

+0

您正在使用哪种XSLT处理器? –

回答

0

使用支持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> 
+0

这就像一个魅力,干杯! –

相关问题