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

August 3rd, 2010, 08:32 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Writing the current(system) date to output file
Hi,
I am working with XSLT to read an input XML file and write out a fixed flat file text output.
I need to put the current date in the first line of the output file. It seems like a simple thing, but I did some investigating and it was not quite clear to me. I found this forum and thought I would ask it here.
Thanks!
RR
|
|

August 3rd, 2010, 08:40 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Well XSLT 2.0 has http://www.w3.org/TR/xpath-functions/#func-current-date and http://www.w3.org/TR/xslt20/#function-format-date.
If you use XSLT 1.0 then consider to pass in the date you are interested as a global parameter or check whether your XSLT processor allows you to write an extension function in a language that has some function to get the current date.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 3rd, 2010, 08:41 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
It's not possible using 'pure' XSLT version 1.0. It's easily done with version 2.0 using current-date().
If you're stuck with version 1.0 then you'll either have to pass it in as a parameter or use an extension function. For example MSXML parser allows you to write extensions using JavaScript.
|
|

August 3rd, 2010, 09:36 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the quick responses!
I am new to XSLT, and am still feeling my way around versions and things like that. Here is what I did:
I am editing in Miscrosoft Visual Studio and running in a command prompt
I commented out version 1.0 commented out and added version 2.0 like this:
<!-- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
I added this commmand:
<xsl:value-of select="current-dateTime()"/>
Nothing was underlined in red, so there was no syntext error with this.
Running in command prompt, I get this error: (Note: I am editing the file names)
C:\XSLT>msxsl RR.xml test.xslt -o RRKW_test.txt
Error occurred while executing stylesheet 'test.xslt'.
Code: 0x80004005
'current-dateTime' is not a valid XSLT or XPath function.
-->current-dateTime()<--
C:\XSLT>
Please let me know if there is anything else to look at.
Thanks again!
RR.
|
|

August 3rd, 2010, 09:43 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
None of Microsoft's offerings support version 2.0. If you want to stick with the MSXML library then you'll need a JavaScript extension function. Otherwise you can move to a version 2.0 processor such as Saxon.
To see how to create a scripted extension download or view the Microsoft Xml Core Services SDK. It's basically an msxsl:script element with a function written in JavaScript.
http://msdn.microsoft.com/en-us/library/ms256042.aspx
|
|

August 3rd, 2010, 09:43 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
The version of XSLT you are using doesn't depend just on the "version" attribute, but also on what XSLT processor you are running.
You are using the Microsoft XSLT Command Line tool which only supports version 1.
There are a number of XSLT processors that support version 2 - notably Saxon and AltovaXML.
|
|

August 3rd, 2010, 02:51 PM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for all the help.
I am trying to incorporate a Java script in the XSLT to accomplish this. I do not know Java, but I found some examples on the internet.
I put something in that works, but I need to get 2 digits for the month and day, and it is only giving me 1 digit.
Any ideas of how to do this?
Here is what I put in, that is giving results of: 8/3/2010
---------------------------------------------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:myJavaScript="urn:internal:my-javascript">
<msxsl:script language="JScript" implements-prefix="myJavaScript">
<![CDATA[
function GetCurrentDateTime()
{
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
return(month + "/" + day + "/" + year);
}
]]>
</msxsl:script>
-----------------------------------------
Thanks again!
RR
|
|

August 4th, 2010, 04:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Add this code to your script block:
Code:
String.prototype.padLeft = function (padToCount, padWith)
{
padWith = padWith || "0";
var s = this.toString();
for (var i = 0, l = s.length; i < padToCount - l; i++)
{
s = padWith + s;
}
return s;
}
Then you can do:
Code:
var month = currentTime.getMonth() + 1;
month = month.toString().padLeft(2);
|
|

August 4th, 2010, 07:36 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I found this on the internet, and it worked. I was still curious to see if you guys had a better idea. Let me know if your way is better.
Funny, how much is needed just to get the current date one time.
Thanks for the tips!!!
----------------------------------------------------
function GetCurrentDateTime()
{
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
if (month <=9){month = "0" + month};
var day = currentTime.getDate();
if (day <=9) {day = "0" +day};
var year = currentTime.getFullYear();
return(month + "/" + day + "/" + year);
-----------------------------------------------------
RR.
|
|
 |