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 July 11th, 2008, 04:53 AM
Authorized User
 
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vsempoux
Default insert current date

Hello all,

in my xslt i would like to insert a datestamp tag
eg: <CreationDate>25/10/2008</CreationDate>
The way i want it, is that the date is put in it automatically when the xml is transformed.
Is this possible ?

Greetings to all

 
Old July 11th, 2008, 05:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 use the current-date() function (and format-date(), if you want it in dd/mm/yyyy format).

In XSLT 1.0 you'll have to pass the current date in as a stylesheet parameter from the calling application, or get it using an extension function call.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2008, 05:10 AM
Authorized User
 
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vsempoux
Default

Thanks,
But how do i call this function within my xslt?
 
Old July 11th, 2008, 05:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

These functions are just like count(), sum(), or substring() - you can use them within any XPath expression, for example the expression in

<xsl:value-of select="EXPR"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2008, 05:38 AM
Authorized User
 
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vsempoux
Default

Ok,
when i try this <xsl:value-of select="current-date()"/> XMLspy tells me "XSL transformation failed due to following error:

'current-date' is not a valid XSLT or XPath function.

-->current-date()<-- "
How can i solve this ?

Thanks,

 
Old July 11th, 2008, 05:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As I said, it's an XSLT 2.0 function. Use a version of XML Spy that supports XSLT 2.0, and make sure that XSLT 2.0 is enabled.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2008, 06:27 AM
Authorized User
 
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vsempoux
Default

Ok, so far so good.
xslt 2.0 enabled and working

for the moment i have <xsl:value-of select="current-date()"/>
which returns me 2008-07-11+02:00.
how do i use the function format-date to get 11-07-2008.
i already tried to do like this
<xsl:value-of-select="format-date(current-date(),'DD-MM-YYYY')/>
but that gives me an error.

Any pointers ?

 
Old July 11th, 2008, 06:42 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

http://www.w3.org/TR/xslt20/#function-format-date

I think it should be something like this:

<xsl:value-of-select="format-date(current-date(),'[D]-[M]-[Y]')/>

/- Sam Judson : Wrox Technical Editor -/
 
Old July 11th, 2008, 06:51 AM
Authorized User
 
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vsempoux
Default

YES YES,

That did it.

I was already looking at another possibility, namely define the current date as a variable and then use the concat and substring functions.
But when i did that i got the error that the variable i made was of type date and not of type string.
i declared my var like this
<xsl:variable name="CD" select="current-date()"/>
is there a simple way to say to xml that i want to use my variable as a string
(i already tried to this <xsl:variable name="CD" select="current-date()" as="string"/>
but that didn't work either :) )
Never the less, many thanks for your help...
V.

 
Old July 11th, 2008, 06:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

'string' isn't a data type, however 'xs:string' probably is (provided 'xs' has been defined as the prefix to the XML Schema namespace.

Alternatively you can just use the string() function to try to convert anything to a string. e.g. select="string(current-date())"

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Current Date s15199d Reporting Services 2 April 3rd, 2006 12:38 AM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
Current Date rgudino Java Databases 1 May 5th, 2005 03:09 AM
How to make insert statments for current db method Access VBA 8 March 23rd, 2005 10:47 PM
Current Date?? morpheus SQL Server 2000 2 November 27th, 2003 04:21 PM





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