Solved

Get Date In XML

Posted on 2004-10-06
4
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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