XSLT 1.0 获取当前日期时间 [英] XSLT 1.0 Get Current DateTime

查看:38
本文介绍了XSLT 1.0 获取当前日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XML 文件中有一个包含以下内容的节点:

I have a node in my XML file containing the following:

2011-12-01T16:33:33Z

我希望使用与上述相同的格式将这一行替换为当前日期和时间.

I wish to take this line and replace it with the current date and time using the same format as shown above.

YYYY-MM-DDTHH:MM:SSZ

YYYY-MM-DDTHH:MM:SSZ

该节点位于声明为x"的命名空间内

The node is within a namespace declared as 'x'

推荐答案

单独使用 XSLT 1.0 无法使用 DateTime .. 在类似的情况下,我求助于脚本编写 .. (C#)

Playing with DateTime is not possible with XSLT 1.0 alone .. In a similar situations I took help of scripting .. (C#)

示例 XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Apple>2011-12-01T16:33:33Z</Apple>
</root>

示例 XSLT:

<?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" xmlns:cs="urn:cs">
  <xsl:output method="xml" indent="yes"/>
  <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
      public string datenow()
     {
        return(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"));
     }
     ]]>
    </msxsl:script>
      <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Apple">
      <xsl:copy>
      <xsl:value-of select="cs:datenow()"/>
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

结果输出:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Apple>2012-02-22T18:03:12Z</Apple>
</root>

脚本可能驻留在同一个文件中(就像我在我的示例 XSLT 代码中那样)或者如果触发 XSLTransformation 的代码是 C#,则在调用位置移动相同的代码:)

The script may reside in a same file (like I have it in my sample XSLT code) or if the code triggering XSLTransformation is C# then move the same code in the calling place :)

这篇关于XSLT 1.0 获取当前日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆