Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何将pubDate格式设置为HH:MM AM/PM?_Xslt_Rss - Fatal编程技术网

Xslt 如何将pubDate格式设置为HH:MM AM/PM?

Xslt 如何将pubDate格式设置为HH:MM AM/PM?,xslt,rss,Xslt,Rss,有没有一种简单的方法来格式化RSS发布日期 <pubDate>Thu, 28 May 2015 08:00:00 -0400</pubDate> 我对日期格式不太熟悉,而且我一辈子都记不起时间格式是什么,所以谷歌对我没有任何帮助。一般的日期解析相当复杂。在标准XPath库3.0中添加了fn:format-date()的反面,但由于一般情况下任务的复杂性,没有成功 恐怕您在这里仅限于字符串操作,这对您来说还不错。例如: <xsl:variable name="hou

有没有一种简单的方法来格式化RSS发布日期

<pubDate>Thu, 28 May 2015 08:00:00 -0400</pubDate>

我对日期格式不太熟悉,而且我一辈子都记不起时间格式是什么,所以谷歌对我没有任何帮助。

一般的日期解析相当复杂。在标准XPath库3.0中添加了fn:format-date()的反面,但由于一般情况下任务的复杂性,没有成功

恐怕您在这里仅限于字符串操作,这对您来说还不错。例如:

<xsl:variable name="hours"   select="xs:integer(substring($input, 18, 2))"/>
<xsl:variable name="minutes" select="substring($input, 21, 2)"/>
<xsl:sequence select="
   if ( $hours ge 12 ) then
      concat($hours - 12, ':', $minutes, 'PM')
   else
      concat($hours, ':', $minutes, 'AM')"/>

错误管理尚未添加,由您自行添加,具体取决于您的输入的质量和同质性。

请尝试以下方法:

XSLT1.0

<xsl:template match="pubDate">
    <xsl:variable name="h" select="substring(., 18, 2)"/>
    <xsl:variable name="m" select="substring(., 21, 2)"/>
    <xsl:variable name="h12" select="($h + 11) mod 12 + 1"/>
    <xsl:variable name="am.pm" select="substring('AMPM', 1 + 2*(number($h) > 11), 2)"/>
    <xsl:value-of select="concat($h12, ':', $m, $am.pm)"/>
</xsl:template>


我相信输入
12:00:00
的正确结果是
12:00PM
,而不是
0:00PM
。类似地,
00:00:00
应该产生
12:00AM
,而不是
0:00AM
。也许。我不知道要求是什么。但是想法是存在的,由JesseEarley来调整它以适应它的需要。@JesseEarley如果你的问题得到了回答,请通过接受答案来结束它。
<xsl:template match="pubDate">
    <xsl:variable name="h" select="substring(., 18, 2)"/>
    <xsl:variable name="m" select="substring(., 21, 2)"/>
    <xsl:variable name="h12" select="($h + 11) mod 12 + 1"/>
    <xsl:variable name="am.pm" select="substring('AMPM', 1 + 2*(number($h) > 11), 2)"/>
    <xsl:value-of select="concat($h12, ':', $m, $am.pm)"/>
</xsl:template>