Solved

Get Date In XML

Posted on 2004-10-06
4
507 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

25 Experts available now in Live!

Get 1:1 Help Now