XSLT将日期格式转换为ISO

时间:2012-05-15 16:31:45

标签: xslt

来自(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>

2 个答案:

答案 0 :(得分:1)

以下是关于如何处理此问题的一些想法。

  1. 您可以像在此示例中一样执行子字符串争论Format a date in XML via XSLT。 您可能需要为自己创建一些“可枚举” 一周中的几天和几个月。

  2. XPath 2.0为您提供XSD types constructor functions和 你可以在子串争用之上从这些中得到一些帮助。

  3. 如果您的变压器支持 EXSLT ,您可以使用 date/time extension functions

  4. EXSLT也有regular expression support你可以 用于将twitter日期解析为块以进行进一步转换 进入ISO日期。

  5. 你可以写任何内容a custom extension function 您用来驱动XSLT为您运行转换的技术。

  6. 话虽如此,没有一种功能“简单”的方法可以在XSLT中实现所需。

答案 1 :(得分:1)

这会将d / m / 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>