Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

I need to add 90 days to the current date populated in the 2nd half of this code. I've attached the resulting XML below (using xml1.0 and xalan 2.7.1), it looks great i just need Licensing_End_Window to read 90days post todays date and can't figure it out for the life of me. Note this is just a snippet of the XSLT and resulting 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>

Resulting 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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I see you are trying to do this with Java extensions. I don't know if and how this can work, but I can suggest a purely (almost) XSLT 1.0 solution. The following stylesheet:

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>

when applied to any XML input (on Dec 11, 2014), returns:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <start>12-11-2014</start>
   <end>03-11-2015</end>
</output>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...