XSLT / XPATh / DateTime algebra possible?

B

blabla120

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
 
J

Johannes Koch

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.
 
B

blabla120

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
 
J

Joe Kesselman

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).
 
M

Martin Honnen

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top