Solved

XML and get system date?

Posted on 2004-09-14
17
1,842 Views
Last Modified: 2013-11-19
Is it possible to get a system date from within a standard XML?  

Something like:

<?xml version="1.0" ?>
<File>
    <Extension Type="EXE">
        <Name="Mytest"/>
    </Extension>
    <Create Date={get system date and populate here}/>
</File>

I'm not if I need an XSLT or DTD for this.

Any assistance would be greatly appreciated.  

Thanks
0
Comment
Question by:davism
  • 6
  • 5
  • 2
  • +2
17 Comments
 
LVL 2

Accepted Solution

by:
kmcorbett2 earned 43 total points
Comment Utility
You can use DTD as a sort of macro processor. You could create an external entity that contains the system date, and pull the entity replacement text into your document. This would entail (re)writing the external entity at run time to contain the system date. This is probably overkill, using a hammer to swat a fly. But let me know if you want sample XML to illustrate this approach.

More commonly, one would use some sort of XML-aware processing program to modify the document. XSLT processors are commonly used for this sort of thing. You could do this with just about any scripting / programming language, and IMO, many languages and APIs are easier to deal with than XSLT.

Are you already using XSLT or another language in your application? If you say more about what you're trying to do, we can probably offer some more concrete suggestions.

/kmc
0
 
LVL 3

Assisted Solution

by:wadalhag
wadalhag earned 41 total points
Comment Utility
This can be done during XSLT transformation using the date extension by James Clark

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                        xmlns:saxon="http://icl.com/saxon"
                        xmlns:date="http://www.jclark.com/xt/java/java.util.Date">

 <xsl:if test="function-available('date:to-string') and function-available('date:new')">
<xsl:value-of select="translate(date:to-string(date:new()),':','')"/>
 </xsl:if>

</xsl:stylesheet>


I have similar questions where by the dates were used to name output files. Look in
http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21126265.html  
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
hm...the questino is not very clear..

do you have existent XML document with no date populated?
or do you want to create XML document and populate date during this process?
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
>wadalhag
your solutino tied to Saxon processor, who said author using Saxon? or Java?
0
 
LVL 3

Expert Comment

by:wadalhag
Comment Utility
Dear dualsoul

The Author never said Saxon or Java. But at the same time he never specified any alternatives.
You don’t know he might like it !!!.
By the way  other hand Saxon is just to convert the date to string and not compulsory to use.
Why you guys at the top are so defensive. This is just a place to share ideas and experiences or I am I wrong  
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
>By the way  other hand Saxon is just to convert the date to string and not compulsory to use.

hm....i open your stylesheet in XMLSpy and doesn't work, beacuse i haven't Saxon installed, so? :)

0
 
LVL 3

Expert Comment

by:wadalhag
Comment Utility
Of course it will not work XMLSpy due to the use the MSXML which isn’t greate at all If you dealing with real transformation not just a display tricks.  
This because of the java awareness not the Saxon
You will need a transformation engine which understand java any thing such as Xalan, Saxon or XT And you don’t have to be a java programmer to use any of them.
By the way Saxon is the only transformation engine which adopt the XSLT2 by the w3c and I am not sure when Microsoft will be able to produce something for it..
When people work with XML they should think XML not XMLSpy or Microsoft.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:wadalhag
Comment Utility
If you really want to use the XMLSpy you can set it up to use some external engine and continue your work as usual.
But the following in the box or command to call any other java aware engine
e.g. java -jar c:\xxx\saxon.jar -o %2 %1 %3
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
wadalhag , certanly i know how to bind external XSLT engine  to XMLSpy :)

I've just show you the scenario - where what you have posted makes no sense.
to make your scenario work i need to install JVM, then install Saxon and set up XMLSpy, i think it's too long process to get something work, especially if there are no need in XSLT :)

But i don't want to argue about it, this is just my opinion :)

0
 
LVL 3

Expert Comment

by:wadalhag
Comment Utility
dualsoul
I think did make sense in another question where the guy who asked the question never used Saxon at all in the past.
http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21098399.html

I am not sure how many PC’s out there without JVM and instant Saxon works with the stranded Microsoft Virtual Machine (Excluding XP).
I am not questioning what you know (you r the 2nd on the list of experts), just trying clarify things so other users know it.
however I do respect your opinion and I would expect the same from you.
0
 
LVL 15

Expert Comment

by:dualsoul
Comment Utility
certanly i respect your opinion wadalhag

just my vision of situatuin, nothing more :)
0
 
LVL 26

Expert Comment

by:rdcpro
Comment Utility
Actually wadalhag, XML Spy does not use MSXML by default, it uses it's own internal engine, though I can certainly use MSXML or an external processor.  And we're not the defensive ones, you're overreacting here.  In fact, dualsoul, in particular, is rather broad minded--for a Java guy...  ;^)  

The truth is I've found that anti-microsoft people like yourself are the defensive ones, and generally do not "share ideas and experiences".  It's your way or the highway.

Your suggestions is very processor-specific, and in posting a solution like that, you should specify to the user that your solution only works with a particular XSLT processor, and is not cross-platform.  I try always to do just that when suggesting an extension function, since they are largely processor dependant.

Now, to answer the OP:

There is no built-in mechanism to get a date into XML, since XML is only a way of representing data.  KmCorbett2's answer works if you want to use a DTD, but that requires resolving external references when the XML is parsed, and so that may not be a solution--it depends on what or where you're doing all this.  And it still requires some sort of server-side programming, or CGI or some such.

XSLT solutions are overkill, if all you want is a date stamp.  Making a platform and processor choice strictly to get the date into an XML document is not a good idea, either.  

If there is no server-side processing/programming system, and you have a static XML file, then it's probably not possible to do this.  This should be done when the XML is created, or if the XML is, or can be dynamically generated (ie: Select * from TableFoo for XML AUTO), then that's the appropriate place to do it.   If you have server-side programming available (describe your platform), then there are DOM techniques to add an element to a document that are simple enough:

If you were actually using XSLT with the XML later in the processing, I have a different approach that I prefer--I set a parameter prior to the transformation with the date and time I want to use.  This allows me to control the exact date and time, and doesn't require that my XSLT use extensions (a potential security risk, and in some environments means my code has to execute with elevated privileges), and lets me take advantage of pre-compiled and cached XSLT.

We need more information about what you're trying to do, and what your environment is.

Regards,
Mike Sharp
0
 
LVL 3

Expert Comment

by:wadalhag
Comment Utility
rdcpro

and you calling me defensive (check your wording).
I am proud of what you just called me (anti-microsoft) everything anti-microsoft people do depends on sharing ideas and as a result 100’s of applications been developed (go and see if Microsoft will share anything with you)  
I am only defensive if I get attacked. (check the sequence)
My solution will work on any platform as java is cross-platform (I am not a .net guy with a fake sharing policy)
Nothing wrong with a specific solutions to give a quick guide to solve the problem (you know which question I am talking about)
I think you are mad at me for taking 2x500 points from you just last week (you didn’t like my answers on both occasion but they did work and I got the points)
You can go on for hours about what right and what wrong (but what works is what right).
I would be great if I don’t get interrupted by any of you guys (I am not harming any of you if you don’t like my answers ignore them and some one else might look at them)

0
 
LVL 1

Author Comment

by:davism
Comment Utility
Wow,

Guys, I wasn't expected this to turn into a battle.  

What I'm trying to get to is a filename that is named with a system date included.  I can't use have the generation of the XML be done differently.  

I can only use values within the XML which none are system date related or can I have the XML be generated with a date tag within it.  

I have a static XML and need to work with it and I want to see about putting a system date in it for when it is used.  

Wadalhag,  are you saying that a potential option is to use XML Spy and Java?  If that's the case, I can't do that.  The system doesn't have XML Spy nor Java.  

rdcpro,  this process is not CGI related or web-related.  It's purely a process running through cscript using wsh and vbs.  So, keep in mind, the solution needs to be taken into account where there is absolutely no exposure to the internet.  

I want to generate something like:

<?xml version="1.0" ?>
<File>
        <Name="Mytest" + {the system date in ccyymmdd} + ".EXE"/>
</File>

Is this possible?

I know VBS or wsh can deal with the system date but is it possible to isolate the solution to the XML and not the VBS or WSH because I can't really change the execution mechanism.  The fact that VBS and WSH are being used is a tell-tale sign that this is on a Windows platform.

Now, as far as the battle...guys, I'm neither pro-Windows, pro-.NET or pro-Java.  I, in fact, have issues with Windows, with .NET and with Java and Open Source.  There are pro's and con's with either and I can probably write a book with respect to those.   One is not better than the other aside from what people think.  It's just a matter of what people are accustomed to and initially exposed to in getting their knowledge.  So, for instance if you know and are familiar with DCL (OpenVMS) you will hate Unix and KSH because you knew and understood how DCL worked and you have to learn something knew and it's not the same or things are done a little differently.  Same with Windows, if you were initially exposed to Windows and you knew the in's and out's of how to leverage it's capabilies then you are just fine with Windows but when exposed to Java you realize that you wished you had Windows to do the same thing in that yes, the general concepts are there but maybe done differently.  Same with Java, if you were initially exposed to Java and knew the in's and out's of how to leverage it's capabilities then you wished you had Java if you were working in Windows unaware there what you are trying to do is capable of being done in Windows...just not the way you may be accustomed to doing it.

I utilize what I have available to me without getting more if not absolutely necessary to do so or prudent to do so.  

I understand the battle but it's not one that anybody can either win or lose.  It's just there (kinda like Chevy vs. Ford) :-)  

Not to sound rude and I very much appreciate the valuable input but is there a way to focus on the question at hand, please.  :-)

Thanks
0
 
LVL 26

Assisted Solution

by:rdcpro
rdcpro earned 41 total points
Comment Utility
Well, XML is not a programming language, so there is nothing built-in that will provide this functionality. KMCorbett2's answer is the closest to doing this, though it will still need some sort of programming to get the job done.

My suggestion, since you're already using it, is to use WSH and vbscript (or javascript).  As I said before, using XSLT for this is overkill, unless you're already using it elsewhere in the process.  Just load the XML document, select the node you want to add a date string to, and add the date.  In javascript, and assuming you want to add the date to:

<?xml version="1.0" ?>
<File>
    <Extension Type="EXE">
        <Name="Mytest"/>
    </Extension>
    <Create Date={get system date and populate here}/><!-- add date here -->
</File>


var xmlDoc = new ActiveXObject("Msxml2.DomDocument");
xmlDoc.load(filespec)
var oNode = xmlDoc.selectSingleNode("File/Create/@Date");
if (oNode != null)
{
      myDate = new Date()
      var YYYY,MM,M,DD,D,hh,h,mm,m,ss,s;  
      YYYY = myDate.getFullYear() + "";
      MM = (M=myDate.getMonth()+1)<10?('0'+M):M;  
      DD = (D=myDate.getDate())<10?('0'+D):D;  

      oNode.nodeValue = YYYY + MM + DD
}

Note that nodeValue only works for attributes and text nodes, using it on an element will throw an error...in your case it's an attribute, so no problem.

Here's a nifty date prototype I've used for this sort of thing, when I want to insert a W3C datetime datatype into an XML node (or anywhere else, for that matter):

Date.prototype.toDateTimeString = function()
{
      var YYYY,MM,M,DD,D,hh,h,mm,m,ss,s;  
      YYYY = this.getFullYear() + "";
      MM = (M=this.getMonth()+1)<10?('0'+M):M;  
      DD = (D=this.getDate())<10?('0'+D):D;  
      h=this.getHours();  
      hh = h<10?('0'+h):h;  
      mm=(m=this.getMinutes())<10?('0'+m):m;  
      ss=(s=this.getSeconds())<10?('0'+s):s;  
      return YYYY + "-" + MM + "-" + DD + "T" + hh + ":" + mm + ":" + ss
}

It's used like:

var Now = new Date()
alert(Now.toDateTimeString());

So you can use this to prototype the date object, and then you have your own custom method you can use to create any type of date string for your node.  VBScript doesn't have this type of OO behavior, as far as I know, so you'll have to translate the above into appropriate VBScript code, if that's the langauge you're using.  FWIW, I've done a number of projects very similar to yours with exactly the same technology.  Even created XML documents on the fly to use to update a remote SQL Server, and the whole thing running as a scheduled task.


As for the "battle", I felt it was important to point out that a certain solution only worked on a single XSLT processor, and we did not yet know what your platform or environment were, or even that you were using XSLT at all.  

Regards,
Mike Sharp
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now