Solved

Get Date In XML

Posted on 2004-10-06
4
509 Views
Last Modified: 2011-09-20
I would like to know if there is a way to get the date using xml, or if there is some way to get the date via javascript or something. Because I need to be able to compare the current date to one in the document is the XSL if statement. Any ideas?
0
Comment
Question by:gf3
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 12245216
Using Extension functions it's possible.  This example uses MSXML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:map="urn:schemas-rdcpro-com:xslt-map"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:dates="urn:rdcpro-com:dates"
            >
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <msxsl:script language="JScript" implements-prefix="dates">
            <![CDATA[
                  function getDate() {
                        var oDate = new Date();
                        return oDate.getFullYear() + "-" + (oDate.getMonth() + 1) + "-" + oDate.getDate();
                  }
            ]]>
      </msxsl:script>
      <xsl:template match="/">
            <h2>Today's Date</h2>
            <xsl:value-of select="dates:getDate()"/>
      </xsl:template>
</xsl:stylesheet>

However, I think it's much better to set the date as a parameter externally, using the addParameter() method or whatever similar mechanism your processor has.  In MSXML, it would be:

var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0");
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
xslDoc.load("sample.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("books.xml");

date = new date();

if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   xslProc = xslt.createProcessor();
   xslProc.input = xmlDoc;
   xslProc.addParameter("param1", date.toDateString());
   xslProc.transform();
   alert(xslProc.output);
}


Extension functions are a security issue, and in some platforms, code that uses them must run with elevated privileges. But they work.  They are not necessarily portable across platforms.


Regards,
Mike Sharp
0
 
LVL 1

Author Comment

by:gf3
ID: 12248404
Sorry I didnt mention this earlier but this application will be linux based. So I don't believe that I have access to MSXML. Is there another way to do this?
0
 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 12254939
Linux is the operating system...the question is, what XSLT processor are you using?  When you mentioned Javascript, I used MSXML as a generalized example.  Many XSLT processors use extension functions, some of them natively. Even Saxon, which doesn't have a scripting engine, can use java.  Take a look at http://exslt.org for a vast library of extensions.    

Personally, as I said, I would probably use the addParameter approach, as the XSLT can be cross-platform that way, and you avoid a few other pitfalls.  It's probably faster, too.  Consult your XSLT processor documentation on the syntax for this.

Regards,
Mike Sharp
0
 
LVL 1

Author Comment

by:gf3
ID: 12427144
Thanks for the help, really sorry about the huge delay. I forgot about and it and i came over it again while cleaning up my inbox. Thanks again for the help!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net and XML XSD Files 1 58
Load XML file into a SQL Server Table using SSIS 2012 9 74
microsoft access - xml 10 65
Insert Powershell variable into XML 4 44
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

10 Experts available now in Live!

Get 1:1 Help Now