繁体   English   中英

SimpleDateFormat和Date:new如何才能简单地报告将来的日期? XSLT,XML1.0,Java

[英]SimpleDateFormat and Date:new how can I simply report a date in the future? XSLT, XML1.0, Java

我需要在此代码第二部分的当前日期中添加90天。 我已经在下面附加了生成的XML(使用xml1.0和xalan 2.7.1),它看起来很棒,我只需要Licensing_End_Window即可读取今天的90天后的日期,并且无法解决我的一生。 请注意,这只是XSLT及其结果XML的片段。

                    <xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_Start</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="s" select="SimpleDateFormat:new('MM-dd-yyyy')"/>
                        <xsl:variable name="Date" select="Date:new()"/>
                        <xsl:choose>
                            <xsl:when test="add:FlightStart"><xsl:value-of select="substring(add:FlightStart,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="SimpleDateFormat:format($s,$Date)" />
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>
                <xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_End</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="s" select="SimpleDateFormat:new('MM-dd-yyyy')"/>
                        <xsl:variable name="Date" select="Date:new()"/>
                        <xsl:choose>
                            <xsl:when test="add:FlightEnd"><xsl:value-of select="substring(add:FlightEnd,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="SimpleDateFormat:format($s,$Date)"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>

产生的XML

 <Name="Licensing_Window_Start" Value="12-10-2014"/><App_Data App="MOD" Name="Licensing_Window_End" Value="12-10-2014"/><App_Data App="MOD"

我看到您正在尝试使用Java扩展来做到这一点。 我不知道它是否可以工作以及如何工作,但是我可以建议一个(几乎)XSLT 1.0解决方案。 以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="today" select="date:date()" />

<xsl:template match="/">
    <output>
        <start>
            <xsl:value-of select="concat(substring($today, 6, 2), '-', substring($today, 9, 2), '-', substring($today, 1, 4))" />
        </start>
        <end>
            <xsl:call-template name="add-days-to-today">
                <xsl:with-param name="days-to-add" select="90" />
            </xsl:call-template>
        </end>
    </output>
</xsl:template> 

<xsl:template name="add-days-to-today">
    <xsl:param name="days-to-add"/>
    <xsl:param name="year" select="substring($today, 1, 4)"/>
    <xsl:param name="month" select="substring($today, 6, 2)"/>
    <xsl:param name="day" select="substring($today, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:param name="JDN" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045 + $days-to-add" />
    <xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
    <xsl:param name="e" select="4*$f + 3"/>
    <xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
    <xsl:param name="h" select="5*$g + 2"/>
    <xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
    <xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
    <xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
    <xsl:param name="MM" select="format-number($M, '00')"/>
    <xsl:param name="DD" select="format-number($D, '00')"/>
    <xsl:value-of select="concat($MM, '-', $DD, '-', $Y)" />
</xsl:template>     

</xsl:stylesheet>

当应用于任何XML输入(2014年12月11日)时,返回:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <start>12-11-2014</start>
   <end>03-11-2015</end>
</output>
<xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_Start</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="today" select="date:date()" />
                        <xsl:choose>
                            <xsl:when test="add:FlightStart"><xsl:value-of select="substring(add:FlightStart,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                <output>
                                    <Licensing_Window_Start>
                                        <xsl:value-of select="concat(substring($today, 6, 2), '-', substring($today, 9, 2), '-', substring($today, 1, 4))" />
                                    </Licensing_Window_Start>
                                </output>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>
                <xsl:element name="App_Data">
                    <xsl:attribute name="App">MOD</xsl:attribute> 
                    <xsl:attribute name="Name">Licensing_Window_End</xsl:attribute> 
                    <xsl:attribute name="Value">
                        <xsl:variable name="today" select="date:date()" />
                        <xsl:choose>
                            <xsl:when test="add:FlightEnd"><xsl:value-of select="substring(add:FlightEnd,1,10)"/></xsl:when>
                            <xsl:otherwise>
                                    <output>
                                        <Licensing_Window_End>
                                            <xsl:call-template name="add-days-to-today">
                                                <xsl:with-param name="days-to-add" select="90" />
                                            </xsl:call-template>
                                        </Licensing_Window_End>
                                    </output>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>

将变量Today和今天的Add-Day-To_Today模板放置在样式表的顶部。 效果很好。 再次感谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM