XSLT转换日期格式为ISO(XSLT convert date format to ISO)

2019-10-16 17:45发布

特(Twitter格式的例子)

Tue, 15 May 2012 15:40:34 +0000

(ISO格式的例子)

2005-12-01T13:45:00

使用XSLT


下面是一个XML片段

<item>
     <title>It's called "Kangaroo Care" - what parents can do to create a special bond and improve the health of their newborns: http://t.co/8MV8FpM4</title>
        <link>http://twitter.com/ProvHealth/statuses/202423233104973826</link>
        <description>It's called "Kangaroo Care" - what parents can do to create a special bond and improve the health of their newborns: &lt;a href="http://t.co/8MV8FpM4"&gt;http://t.co/8MV8FpM4&lt;/a&gt;</description>
     <pubDate>Tue, 15 May 2012 15:40:34 +0000</pubDate>
        <guid>http://twitter.com/ProvHealth/statuses/202423233104973826</guid>
        <author>ProvHealth@twitter.com (Providence Oregon)</author>
     <media:content type="image/jpg" height="48" width="48" url="http://a0.twimg.com/profile_images/748948431/cross_normal.png"/>
     <google:image_link>http://a0.twimg.com/profile_images/748948431/cross_normal.png</google:image_link>
        <twitter:metadata>
         <twitter:result_type>recent</twitter:result_type>
     </twitter:metadata>
 </item>

Answer 1:

以下是有关如何处理这一些想法。

  1. 你可以做在这个例子中闹得不可开交 ,如: 通过XSLT格式化XML的日期 。 你可能需要创建自己的一周和月的天数“可枚举”。

  2. XPath 2.0中为您提供了XSD类型的构造函数 ,你可以从这些得到一些帮助在子争吵的顶部。

  3. 如果你的变压器支持EXSLT你可以用走的日期/时间扩展功能 。

  4. EXSLT也有正则表达式的支持,你可以用它来分析Twitter的日分成块用于进一步转化成ISO日期。

  5. 你可以写一个自定义的扩展功能,在你使用任何技术来驱动你的XSLT来运行转换为您服务。

随着中说,有没有一个功能“轻松”的方式来完成你的XSLT所需要的。



Answer 2:

这个转换d /米/ YYYY HH:MM:SS AM ISO日期。 没有时间在这呢,但是这将是这方面的一个延续。 事实上,日和月可能是一个字符或两个复杂的。

  <!--Convert from 'd/m/yyyy hh:mm:ss AM' to YYYY-MM-DD-->
  <xsl:template name="ISODate">
    <xsl:param name="Date" />
    <xsl:param name="Year" />
    <xsl:param name="Month" />
    <xsl:param name="Day" />

    <xsl:choose>
      <xsl:when test="not($Month)">
        <xsl:call-template name="ISODate">
          <xsl:with-param name="Month" select="substring(concat('0',substring-before($Date,'/')), string-length(concat('0',substring-before($Date,'/'))) - 1, 2)" />
          <xsl:with-param name="Date" select="substring-after($Date,'/')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="not($Day)">
        <xsl:call-template name="ISODate">
          <xsl:with-param name="Month" select="$Month" />
          <xsl:with-param name="Day" select="substring(concat('0',substring-before($Date,'/')), string-length(concat('0',substring-before($Date,'/'))) - 1, 2)" />
          <xsl:with-param name="Date" select="substring-after($Date,'/')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="not($Year)">
        <xsl:call-template name="ISODate">
          <xsl:with-param name="Month" select="$Month" />
          <xsl:with-param name="Day" select="$Day" />
          <xsl:with-param name="Year" select="substring-before($Date,' ')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($Year,'-',$Month,'-',$Day)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


文章来源: XSLT convert date format to ISO
标签: xslt