Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Date In XML

Posted on 2004-10-06
4
Medium Priority
?
519 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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. 
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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