XSLT / XPATh / DateTime algebra possible?

Discussion in 'XML' started by blabla120@gmx.net, May 5, 2006.

  1. Guest

    Hello,

    I have a source file with

    <StartEvent starttime="2006-04-19T20:15:00Z"/>

    I want to transform it with XSLT into

    <Starttime>2006-04-21T20:15:00Z</Starttime>

    The clue:
    Onto the date, there should be added 2 days.

    Is that possible?

    Thanks.
    Bernd
     
    , May 5, 2006
    #1
    1. Advertising

  2. wrote:

    > I have a source file with
    >
    > <StartEvent starttime="2006-04-19T20:15:00Z"/>
    >
    > I want to transform it with XSLT into
    >
    > <Starttime>2006-04-21T20:15:00Z</Starttime>
    >
    > The clue:
    > Onto the date, there should be added 2 days.


    Use string functions (substring...) to get the parts of the date, then
    add 2 to the day, and concatenate the (new) parts.
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, May 5, 2006
    #2
    1. Advertising

  3. Guest

    Hi Johannes,

    this would not work properly, e.g. when you have 31 as the day, your
    function would make a 33 of it.....

    Bernd
     
    , May 5, 2006
    #3
  4. wrote:

    > this would not work properly, e.g. when you have 31 as the day, your
    > function would make a 33 of it.....


    Then add this to your algorithm.
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, May 5, 2006
    #4
  5. XSLT 1.0 doesn't have date arithmetic built into it.

    You can implement it yourself (algorithms are available on the net; I'm
    fond of working in terms of astronomical/Julian day numbers though that
    may be overkill for your needs), or if you don't care about portability
    to other processors you may want to do this by calling out to extension
    functions that leverage existing implementations (eg Java's date classes).

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, May 5, 2006
    #5
  6. wrote:


    > I have a source file with
    >
    > <StartEvent starttime="2006-04-19T20:15:00Z"/>
    >
    > I want to transform it with XSLT into
    >
    > <Starttime>2006-04-21T20:15:00Z</Starttime>
    >
    > The clue:
    > Onto the date, there should be added 2 days.
    >
    > Is that possible?


    XSLT/XPath 2.0 can do that, here is an example stylesheet

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
    exclude-result-prefixes="xs xdt"
    version="2.0">

    <xsl:eek:utput indent="yes" />

    <xsl:template match="StartEvent">
    <Starttime><xsl:value-of select="xs:dateTime(@starttime) +
    xdt:dayTimeDuration('P2D')" /></Starttime>
    </xsl:template>

    </xsl:stylesheet>

    Result with Saxon 8 from <http://www.saxonica.com/> is e.g.

    <Starttime>2006-04-21T20:15:00Z</Starttime>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 5, 2006
    #6
  7. Guest

    That's what I needed, thanks a lot Martin!
     
    , May 5, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    719
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    544
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    397
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    343
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    765
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page