Subject: Re: [xsl] Calling mutator java method (date fields to seconds-since-epoch with java.util.GregorianCalendar) From: "Tim Lebo" <timleboxslt@xxxxxxxxx> Date: Tue, 22 Aug 2006 06:58:42 -0400 |
I would much prefer to perform this in XPath. But, to be honest, I have found the typing system to be quite obtuse. Flipping through Appendix B of MK's XSLT 2.0 book, my best guess would be to create two dateTimes: one with my string date fields and one of 1970. Then I could use subtract-dates/dateTimes... to get an xdt:*Duration. But if I had the duration, how would I convert it into the number of seconds?
Despite my best efforts, I have not found a reasonable introduction to the xs/xdt typing system. Does anyone have any suggestions for references?
Regards, Tim
The way extension functions work is to a large extent implementation-defined, so it's fairly meaningless to ask questions about them without saying what XSLT processor you are using.
In general, though, calling extension functions with side-effects is likely to be problematic, as the calls can easily be optimized away.
If this is Saxon, for example, a variable that's never referenced will never be evaluated. And variables that are used will usually be evaluated in a different order from the order in which they are declared.
With Saxon you can usually ensure that a void extension function is called by including the call in an xsl:value-of instruction that is writing to the result tree. Saxon deliberately avoids looking at the function signature to discover that the method always returns void, instead treating it as if the returned result might be significant.
But if you're using Saxon, why not do the calculation in XPath?
Michael Kay http://www.saxonica.com/
> -----Original Message----- > From: Tim Lebo [mailto:timleboxslt@xxxxxxxxx] > Sent: 22 August 2006 02:35 > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: [xsl] Calling mutator java method (date fields to > seconds-since-epoch with java.util.GregorianCalendar) > > Hello, > > I need to determine the number of seconds past the epoch > given the year, month, day, hour, and minute fields (as strings). > > This can be done with the java.util.GregorianCalendar class using the > set(year,month,day,hour,minute) mutator and calling the > getTimeInMillis() accessor. I have been able to demonstrate > this in a sample main class that uses GregorianCalendar: > > // For year,month,day,hour,min -> > seconds since epoch > import java.util.Calendar; > import java.util.GregorianCalendar; > > public class MyDate { > > public static void main(String args[]) { > Calendar myCal = new > GregorianCalendar(); > myCal.set(2006,7,21,13,26,0); > //1156181207 > > System.out.println(myCal.getTimeInMillis()/1000 - 1156181207); > } > > } > > > The following xslt shows how I instantiate a > GregorianCalendar. Two outputs are obtained: > 1) A failed attempt to get the seconds-since-the-epoch after > the set method call > 2) The objects toString, which shows that the date > information is actually the time of execution and not the date set > > This leads me to believe that the mutator call did not 'take effect'. > Looking to previous java-interaction solutions that I have > created, I notice that this is the first time I am calling an > object's mutator method, which returns void. Does this need > to be handled differently? > > Regards, > Tim > > > > <xsl:transform version="2.0" > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > > xmlns:xfm="transform namespace" > > xmlns:xs="http://www.w3.org/2001/XMLSchema" > > xmlns:cal="java:java.util.GregorianCalendar" > exclude-result-prefixes="xfm xs"> > > <xsl:output method="text"/> > > > > <xsl:template match="/"> > > <xsl:variable name="start" select="cal:new()"/> > > <xsl:variable name="start-set" > select="cal:set($start,xs:integer('2006'), > > > > > xs:integer('7'),xs:integer('21'), > > > > > > xs:integer('13'),xs:integer('26'),xs:integer('0'))"/> > > <xsl:value-of select="concat('expecting > seconds-since-epoch of 2006 > 21 Aug at 13:26:00, but getting current seconds-since-epoch: ',$NL, > > > xs:integer(cal:getTimeInMillis($start)) > div xs:integer('1000'),$NL,$NL)"/> > > <xsl:value-of select="concat('toString indicates that > the call to set the date was not recognized (See > DAY_OF_MONTH,HOUR_OF_DAY...):',$NL, > > cal:toString($start),$NL)"/> > > </xsl:template> > > > > <xsl:variable name="NL"> > > <xsl:text> > > </xsl:text> > > </xsl:variable> > > > > </xsl:transform>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Calling mutator java meth, Michael Kay | Thread | Re: [xsl] Calling mutator java meth, David Carlisle |
Re: [xsl] change element to sibling, Mukul Gandhi | Date | Re: [xsl] Calling mutator java meth, David Carlisle |
Month |