Solved

Date problem

Posted on 2004-04-10
10
281 Views
Last Modified: 2008-02-01
Hi Experts,

I have the following example :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"  xmlns:js="javascript:code">
     <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:param name="fromDate"/>
      <xsl:param name="toDate"/>
     <msxsl:script language="jscript" implements-prefix="js">
          <![CDATA[
          function matches(indate,fromdate,todate)
          {
               indate2 = new Date(indate);
               fromdate2 = new Date(fromdate);
               todate2 = new Date(todate);
               return (indate2 <= todate2 && fromdate2 <= indate2);
          }
          ]]>
     </msxsl:script>
      <xsl:template match="/root/node">
           <xsl:variable name="indate"><xsl:value-of select="@indate"/></xsl:variable>
           <xsl:if test="js:matches(substring-before(@indate,' '),string($fromDate), string($toDate))">
                <td><xsl:value-of select="."/></td>
          </xsl:if>
     </xsl:template>
</xsl:stylesheet>

This code is running inside of IFRAME. I intend to pass toDate and fromDate from outside into this IFRAME.

The questions are :

1 how do I pass  $fromDate and $toDate values or where they coming from in this sample
2 I have "indate" format as dd/mm/ccyy 12:00 ET . Will it still work using this code ? If not what has to be modified ?

Thank you.
0
Comment
Question by:fpoyavo
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 15

Expert Comment

by:dualsoul
ID: 10801065
as i understand this XSLT processing you do on client-side?

if so, for MSIE you can set up stylesheet parameter, using addParameter method on XslTransformer.


// Setup fromdate parameter
            xslProcessor.addParameter("fromDate","10/10/2004");
//Setup toDate parameter
                xslProcessor.addParameter("toDate","11/10/2004");

then you can call transform() method to update page:
               xslProcessor.transform();


0
 
LVL 15

Expert Comment

by:dualsoul
ID: 10801085
>2 I have "indate" format as dd/mm/ccyy 12:00 ET . Will it still work using this code ? If not what has to be modified ?

no it should work fine, if you will compare:
         indate=10/10/2004 12:00 ET  with fromData=10/10/2004

no problem :)
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10801448
Yep...but why this code does not work ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:dualsoul
ID: 10801958
what doesn't work specificly?
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10801982
When I try to assign date hard coded date values just to test it the result set is empty.
I tdoes not compare dates for some reason.
0
 
LVL 4

Expert Comment

by:maverick65
ID: 10803052
I have already postet a function to pass the parameters to the xslt in your last question. You must be aware, that this xslt only works togetter with MSXML and not with any other Parser.

I have you now a complete Example here Put all three files into the same directory. Name them Sample.htm, Sample.xml and Sample.xsl.
Then load the Html file into IE. This Example is for MSXML 4 only. When you use MSXML 3.0 then you must load the 3.0 versions in new ActiveXObject.
I can run the example without any problem. If it does not run at yours then something is wrong with your configuration (Parser or Browser).

1. HTML- File:

<html>
      <head>
            <script language="JavaScript">
function Parse(fromDate, toDate) {
      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("sample.xml");
      if (xmlDoc.parseError.errorCode != 0) {
            var myErr = xmlDoc.parseError;
            alert("You have error " + myErr.reason);
      } else {
            xslProc = xslt.createProcessor();
            xslProc.input = xmlDoc;
            xslProc.addParameter("fromDate", fromDate);
            xslProc.addParameter("toDate", toDate);
            xslProc.transform();
            document.write(xslProc.output);
      }
}
      
            </script>
      </head>
      <body>
            <table border="1">
                  <tr>
                        <script> Parse('12/11/1999','15/11/1999');</script>
                  </tr>
            </table>
      </body>
</html>




2. Sample.xml File
<?xml version="1.0" encoding="UTF-8"?>
<root>
      <node indate="12/11/1999 12:00 ET">node1</node>
      <node indate="13/11/1999 12:00 ET">node2</node>
      <node indate="14/11/1999 12:00 ET">node3</node>
      <node indate="15/11/1999 12:00 ET">node4</node>
      <node indate="16/11/1999 12:00 ET">node5</node>
</root>



3. Sample.xsl File
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"  xmlns:js="javascript:code">
     <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:param name="fromDate"/>
      <xsl:param name="toDate"/>
      <msxsl:script language="jscript" implements-prefix="js">
          <![CDATA[
          function matches(indate,fromdate,todate)
          {
               indate2 = new Date(indate);
               fromdate2 = new Date(fromdate);
               todate2 = new Date(todate);
               return (indate2 <= todate2 && fromdate2 <= indate2);
          }
          ]]>
     </msxsl:script>
      <xsl:template match="/root/node">
           <xsl:variable name="indate"><xsl:value-of select="@indate"/></xsl:variable>
           <xsl:if test="js:matches(substring-before(@indate,' '),string($fromDate), string($toDate))">
                <td><xsl:value-of select="."/></td>
          </xsl:if>
     </xsl:template>
</xsl:stylesheet>
 
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10803289
Thank you Man.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10803407
Your example works fine but when I use my xml file it does not.
Here is file :

<?xml version="1.0"?>
 <!DOCTYPE Records [
     <!ELEMENT Records (Record*)>
     <!ELEMENT Record (category, attribute*)>
     <!ELEMENT category (#PCDATA)>
     <!ELEMENT attribute (#PCDATA)>
     <!ATTLIST attribute
     name CDATA #REQUIRED
>
]>
<Records>
     <Record>
          <category>BankData</category>
          <attribute name="custid"><![CDATA[028989287766]]></attribute>
          <attribute name="fname"><![CDATA[John]]></attribute>
          <attribute name="lname"><![CDATA[smith]]></attribute>
          <attribute name="indate"><![CDATA[2003/05/02 00:00]]></attribute>
          <attribute name="accno"><![CDATA[0192883762]]></attribute>
     </Record>
    <Record>
          <category>BankData</category>
          <attribute name="custid"><![CDATA[028433387766]]></attribute>
          <attribute name="fname"><![CDATA[Mike]]></attribute>
          <attribute name="lname"><![CDATA[Lennon]]></attribute>
          <attribute name="indate"><![CDATA[2003/05/01 00:00]]></attribute>
          <attribute name="accno"><![CDATA[0142883763]]></attribute>
     </Record>

</Records>

in your xslt probably something has to be changed.
Would you please help me.



0
 
LVL 4

Accepted Solution

by:
maverick65 earned 500 total points
ID: 10803528
you need to adjust it to your data:
<xsl:template match="/root/node">                                                                      =>             <xsl:template match="/Records/Record">
<xsl:variable name="indate"><xsl:value-of select="@indate"/></xsl:variable>        =>              <xsl:variable name="indate"><xsl:value-of select="attribute[@name='indate']"/></xsl:variable>

Your Dataformat has changed from dd/mm/yyyy to yyyy/mm/dd. Therefore the JAvaScript function is not valid anymore. You have to change the order of your date in the function. But I would succest you to use the second solution I gave you without JavaScript in the XSL:

Try this one instead:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:param name="fromDate"/>
      <xsl:param name="toDate"/>
      <xsl:template match="/Records/Record">
           <xsl:variable name="indate2">
                <xsl:call-template name="convert">
                     <xsl:with-param name="date" select="attribute[@name='indate']"/>
                </xsl:call-template>
           </xsl:variable>
           <xsl:variable name="fromdate2">
                <xsl:call-template name="convert">
                     <xsl:with-param name="date" select="$fromDate"/>
                </xsl:call-template>
           </xsl:variable>
           <xsl:variable name="todate2">
                <xsl:call-template name="convert">
                     <xsl:with-param name="date" select="$toDate"/>
                </xsl:call-template>
           </xsl:variable>
           <xsl:if test="$fromdate2 - 1 &lt; $indate2 and $indate2 &lt; $todate2 + 1">
                    <tr>
                      <td><xsl:value-of select="category"/></td>
                      <td><xsl:value-of select="attribute[@name='custid']"/></td>
                      <td><xsl:value-of select="attribute[@name='fname']"/></td>
                      <td><xsl:value-of select="attribute[@name='lname']"/></td>
                      <td><xsl:value-of select="attribute[@name='indate']"/></td>
                      <td><xsl:value-of select="attribute[@name='accno']"/></td>
                </tr>
          </xsl:if>
     </xsl:template>
     
     <xsl:template name="convert">
          <xsl:param name="date"/>
          <xsl:variable name="dd">
               <xsl:value-of select="substring($date,9,2)"/>
          </xsl:variable>
          <xsl:variable name="mm">
               <xsl:value-of select="substring($date,6,2)"/>
          </xsl:variable>
          <xsl:variable name="yyyy">
               <xsl:value-of select="substring($date,1,4)"/>
          </xsl:variable>
          <xsl:value-of select="$yyyy"/>
          <xsl:value-of select="$mm"/>
          <xsl:value-of select="$dd"/>
     </xsl:template>
</xsl:stylesheet>



You have to change the format of the date in the HTML to yyyy/mm/dd otherwise it wont work.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 10803617
Maverick,

Thank you very much. I am impressed.
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

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…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

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