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

July 11th, 2008, 04:53 AM
|
Authorized User
|
|
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 11th, 2008, 05:01 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

July 11th, 2008, 05:10 AM
|
Authorized User
|
|
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks,
But how do i call this function within my xslt?
|

July 11th, 2008, 05:35 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

July 11th, 2008, 05:38 AM
|
Authorized User
|
|
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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,
|

July 11th, 2008, 05:53 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|

July 11th, 2008, 06:27 AM
|
Authorized User
|
|
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 ?
|

July 11th, 2008, 06:42 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|

July 11th, 2008, 06:51 AM
|
Authorized User
|
|
Join Date: Jul 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

July 11th, 2008, 06:57 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
'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 -/
|
|
 |