XSLT转换日期时间

时间:2017-05-03 11:33:36

标签: xml xslt xslt-1.0

我需要在xslt中进行日期时间转换,EffectiveDate应该是当前日期的第二天,ExpirationDate应该是EffectiveDate +一年你能帮我吗

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<EffectiveDate>2017-05-4 13:05:658+0200</EffectiveDate>
<ExpirationDate>2018-05-4 13:05:658+0200</ExpirationDate>
</root>

输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <root>
            <EffectiveDate></EffectiveDate>
            <ExpirationDate></ExpirationDate>
        </root>
    </xsl:template>
</xsl:stylesheet>

示例xslt:

    declare @var varchar(30)
set @var=' Leon: My Job; New jobs;;;'


select Substring(substring (@var,charindex(':',@var)+1,Len(@var)),0,
CHARINDEX('; ',substring (@var,charindex(':',@var)+1,Len(@var))))

1 个答案:

答案 0 :(得分:0)

This depends on you XSLT processor, but you can solve this problem using a script. This example uses C#.

<?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" xmlns:user="urn:my-scripts">
  <msxsl:script language="C#" implements-prefix="user">
    <msxsl:assembly name="System.Web" />
    <msxsl:using namespace="System.Web" />
    <![CDATA[
          public string GetDate(string DateFormat)
          {
            return DateTime.Now.ToString(DateFormat);
          }

          public string GetExpirationDate(string DateFormat)
          {
            return DateTime.Now.AddYears(1).ToString(DateFormat);
          }

        ]]>
  </msxsl:script>

  <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" />

  <xsl:template match="/">
    <root>
      <EffectiveDate>
        <xsl:value-of select="user:GetDate('dddd, dd MMMM yyyy')"/>
      </EffectiveDate>
      <ExpirationDate>
        <xsl:value-of select="user:GetExpirationDate('dddd, dd MMMM yyyy')"/>
      </ExpirationDate>
    </root>
  </xsl:template>

</xsl:stylesheet>