Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 14th, 2012, 11:33 AM
Authorized User
Join Date: Nov 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default Setting java objects´ properties


I´m trying to get current date from epoch time. So I have a xsl variable with number of days since 1970 and I want to get the current date in yyyyMMdd format.

Because I have to process this operation massively, I would like to make it the efficient way.

I was trying to run this code with XSLT/Saxon:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:GregCal="java.util.GregorianCalendar">
<xsl:variable name="cal" select="GregCal:getInstance()"/>
<xsl:variable name="temp" select="GregCal:setTimeInMillis($cal, $numberOfDays * 86400000)"/><!-- 24*60*60*1000 = 86400000 -->
and get YEAR, MONTH, and DAY from "cal" object.

QUESTION IS: If I use 6.5.5 Saxon it works nice, but if I put the 9.4 EE it ignores the setting method, so "cal" object has always current date (setTimeInMillis is never applied).

Could anyone help me to solve it with 9.4, please?

Thanks a lot. Bye!

Last edited by sportinguista; November 14th, 2012 at 11:49 AM..
Old November 14th, 2012, 12:52 PM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

The best place for Saxon-specific questions is the developer community site at http://saxonica.plan.io/

The Saxon optimizer doesn't evaluate a variable if its value is never used. If you need to call an external method for its side-effects, put it in an xsl:value-of where the result of the method appears to be used (even if it's always predictably empty).

However, there's no need for an extension function call here. Doing xs:dateTime('1970-01-01T00:00:00') + xs:duration('PT1D') * $numberOfDays) gives you the answer directly.
Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Old November 15th, 2012, 05:51 AM
Authorized User
Join Date: Nov 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts

Thank you very much, Michael. It´s my first post and it´s certainly an honour for me to be answered by you.

Finally I made this:

Added namespace: xmlns:xs="http://www.w3.org/2001/XMLSchema"
<xsl:variable name="period" select="concat('P',$numberDays,'D')"/>
<xsl:variable name="duration" select="xs:duration($period)"/>
<xsl:variable name="finalDate" select="xs:dateTime('1970-01-01T00:00:00') + xs:dayTimeDuration($duration)"/>
<xsl:variable name="formattedDate" select="substring-before($finalDate,'T')"/>
<xsl:value-of select="translate($formattedDate,'-','')"/>

What do you think? Where could I further optimize?

Thank you, Michael :)
Old November 15th, 2012, 06:53 AM
Friend of Wrox
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts

I would use the arithmetic operations defined on the date and dayTimeDuration data types to compute the date, and then I would use the format-date function to format the output:
<xsl:output method="text"/>

<xsl:param name="numberOfDays" as="xs:integer" select="365 * 40"/>

<xsl:template match="/">
  <xsl:variable name="finalDate" 
    select="xs:date('1970-01-01Z') + xs:dayTimeDuration('P1D') * $numberOfDays"/>
  <xsl:value-of select="format-date($finalDate, '[Y0001][M01][D01]')"/>

That's basically Mike's suggestion, only using xs:date (as you seem to be interested only in a date but not the time component), plus using format-date to output the yyyyMMdd format you want.
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Old November 15th, 2012, 07:42 AM
Authorized User
Join Date: Nov 2012
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts

Wow! works perfect, Martin.

Thank you too!!

EDIT: Ops! seems this solution is much less efficient than previous :(

Key is "format-date" function. I think I´ll use:

<xsl:value-of select="translate(substring-before($finalDate, 'Z'),'-','')"/>

Last edited by sportinguista; November 15th, 2012 at 07:54 AM..

Similar Threads
Thread Thread Starter Forum Replies Last Post
setting Collection properties for user control PurpleHaze VS.NET 2002/2003 2 December 30th, 2008 02:41 AM
java.lang.RuntimeException: System properties file mshah101 Apache Tomcat 1 November 22nd, 2007 12:17 AM
Problem setting request properties Einherj J2EE 0 January 22nd, 2006 11:00 PM
Java Properties file allang Servlets 1 July 19th, 2004 08:51 PM
Setting the border properties in datagrid. katsarosj ASP.NET 1.0 and 1.1 Basics 2 November 11th, 2003 01:16 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.