如何在XSLT中反转日期格式?

时间:2009-11-18 13:33:08

标签: asp.net xml xslt

我正在网站上工作在该网站中,有些网页通过XSLT从XML获取数据。但是日期显示为YYYY-MM-DD,理想情况下是从这种格式的XML中获取。我想通过XSLT或其他一些方式将此格式转换为DD-MM-YYYY。

请建议我继续前进或向我提供实现此目标的代码。 这是xml给出的格式

 <published date="2009-09-28T07:06:00 CET" />

我希望将其转换为

 <published date="28-09-2009T07:06:00 CET" />

这是xsl文件

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
          <tr>
            <th width="15%" class="bdr">Date</th>
            <th class="bdr">Title</th>
          </tr>
          <xsl:for-each select="hexML/body/press_releases/press_release">
          <xsl:if test="contains(published/@date, '2009')">
            <tr>
              <td valign="top">
                <xsl:value-of select="substring-before(published/@date, 'T')"/>
              </td>
              <td valign="top">
              <a href="result-page.aspx?ResultPageURL={location/@href}"><xsl:value-of select="headline"/></a>
              </td>
            </tr>
            </xsl:if>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

现在告诉我解决方案?这可能是 fn:reverse

4 个答案:

答案 0 :(得分:3)

如果XML的格式为YYYY-MM-DD,则应该能够使用Xpath的tokenize函数将发生-的字符串拆分,然后重新排序。类似于:

<xsl:variable name="dt" value="tokenize(Date, '-')"/>
<xsl:value-of select="concat(dt[3],'-',dt[2],'-',dt[1])"/>

这只是我的头脑(并且未经测试),但你得到了一般的想法。你应该能够分开日期并重新排序。

答案 1 :(得分:2)

假设

<xml>
  <date>2009-11-18</date>
</xml>

这个XSLT 1.0解决方案可以做到这一点:

<xsl:template match="date">
  <xsl:copy>
    <xsl:value-of select="
      concat(
        substring(., 9, 2),
        '-',
        substring(., 6, 2),
        '-',
        substring(., 1, 4)
      )
    " />
  </xsl:copy>
</xsl:template>

如果您的日期可以

<xml>
  <date>2009-11-1</date>
</xml>

你必须使用稍微复杂的

<xsl:template match="date">
  <xsl:copy>
    <xsl:value-of select="
      concat(
        substring-after(substring-after(., '-'), '-'), 
        '-',
        substring-before(substring-after(., '-'), '-'), 
        '-',
        substring-before(., '-')
      )
    " />
  </xsl:copy>
</xsl:template>

答案 2 :(得分:1)

您也可以使用模板。

   <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:template match="/">

    <html>
      <body>
        <table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
          <tr>
            <th width="15%" class="bdr">Date</th>
            <th class="bdr">Title</th>
          </tr>
         <!-- <xsl:for-each select="hexML/body/press_releases/press_release">-->
            <xsl:if test="contains(published/@date, '2009')">
              <tr>
                <td valign="top">
                  <xsl:call-template name="FormatDate">
                    <xsl:with-param name="DateTime" select="published/@date"/>
                  </xsl:call-template>
                </td>
                  <td valign="top">
                  <a href="result-page.aspx?ResultPageURL={location/@href}">
                    <xsl:value-of select="headline"/>
                  </a>
                </td>
              </tr>
            </xsl:if>
          <!--</xsl:for-each>-->
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="FormatDate">
    <xsl:param name="DateTime"/>
        <xsl:value-of select="substring($DateTime,9,2)"/>-<xsl:value-of select="substring($DateTime,6,2)"/>-<xsl:value-of select="substring($DateTime,1,4)"/><xsl:text> CET</xsl:text>
  </xsl:template>
</xsl:stylesheet>

答案 3 :(得分:0)

您似乎需要使用XSLT 2.0架构感知处理器来获得对xs:dateTime数据类型和format-date函数的内置支持。

有关XSLT 2.0能够解析您拥有的字符串的要求,请参阅http://www.w3.org/TR/xmlschema-2/#dateTime

  

dateTime的词汇空间   由有限长度的序列组成   形式的字符:' - '? yyyy' - '   mm' - 'dd'T'hh':'mm':'ss('。'   S +)? (ZZZZZZ)?

请参阅http://www.dpawson.co.uk/xsl/rev2/dates.html#d16685e16以生成输出。

  

format-date(xs:date(       CONCAT(         子($ d,1,4),         ' - ',         子($ d,7,2),         ' - ',         子($ d,5,2)))       '[D01] [MNn] [Y0001]')