File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes How to use the year-from-dateTime function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to use the year-from-dateTime function" Watch "How to use the year-from-dateTime function" New topic
Author

How to use the year-from-dateTime function

Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Hi

I am trying to use fn:year-from-dateTime to get year from a variable that consists of "29/07/09" as below

<xsl:value-of select="fn:year-from-dateTime(xs:date('29/07/09'))">

where 'fn' prefix refers to http://www.w3.org/2005/02/xpath-functions
and 'xs' prefix refers to http://www.w3.org/2001/XMLSchema

and I am getting below error while running transformation from java...

'The first argument to the non-static Java function 'date' is not a valid object reference.'
FATAL ERROR: 'Could not compile stylesheet'



Can some one help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

Are you using an implementation of XSLT which supports functions in this namespace? Or to put it another way, does xs:date work with other inputs but just not with this input, or does it never work?
Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Hi

It doesnt work with any input, any format.. I actually use a variable in place of "29/07/09" string and it doesnt work either


Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

So what leads you to believe that your XSLT implementation supports functions in this namespace at all?

I'm not saying it doesn't; but since that namespace was designed long after XSLT was, it is quite possible that your transformer doesn't support it. I'm just asking what documentation you read which tells you that it does or doesn't.
Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Thanks for the inputs...

I am naive user of xslt and haven't read any document to confirm that the transformer supports this, I just referred the w3schools website for getting the year of a value and using it.

Frankly I am blank on whether it is supported or not. I have xmlapis.jar, xalan.jar and the xerces.jar in my classpath and just running the transformer on the xsls edited with the year-from-dateTime function.

Can you suggest anything on it
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

That's what I suspected. The w3schools tutorial is shamefully missing information about which functions work with XSLT 1.0 and which functions work with XSLT 2.0. Don't waste too much time using that as your primary reference, get yourself a proper book.
Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Yes, I need to get to the basics first....

So, is there any thing that you can help me on this as a work around... I simply can't get away out of this...

If I cant make anything what you say I wouldnt trouble you again


Thanks
Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Hi

I need to tell one more thing... I havent evaluated whether the year-from-dateTime function actually works...I havent got enough to that aspect yet.

In the statement

<xsl:value-of select="year-from-dateTime(xs:date('29/07/2009'))">, xs:date itself is failing not to talk of year-from-dateTime function..


Any help would be appreciated.

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

So you can't use either of those functions, then. How about just using string functions to extract the year part of the date?
Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
Well, I just checked with the substring function and it actually works.

Ram Korutla
Ranch Hand

Joined: Dec 24, 2007
Posts: 80
However, in my specific case, I wouldnt be knowing the kind of format of the date from which I need to extract the year and so on.

So irrespective of the format I need to extract the year, month, etc components and thus I believe I have to go with year-from-dateTime sort of options.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

It's possible to call Java code from an XSLT. But you won't find that covered in the web tutorials (along with a lot of other XSLT basics). Sorry, I'm afraid you're going to have to get a proper reference for that too.

Or alternatively you could use something which supports XSLT 2.0, as your Java code would have the same problem of not knowing the format of the date. (I'm not sure how you expect the xs:date function to deal with that problem but let's leave that aside for now.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use the year-from-dateTime function
 
Similar Threads
Output a date with timezone, in W3C datetime format?
Problem in using date in WHERE clause in SELECT query in Servlet.
rich:hotKey worked when using jQuery?
Error while using xs:dateTime in an XSL and transforming it using a Java program
resultset is not working properly