Date problem

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.
LVL 1
fpoyavoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
maverick65Connect With a Mentor Commented:
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
 
dualsoulCommented:
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
 
dualsoulCommented:
>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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
fpoyavoAuthor Commented:
Yep...but why this code does not work ?
0
 
dualsoulCommented:
what doesn't work specificly?
0
 
fpoyavoAuthor Commented:
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
 
maverick65Commented:
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
 
fpoyavoAuthor Commented:
Thank you Man.
0
 
fpoyavoAuthor Commented:
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
 
fpoyavoAuthor Commented:
Maverick,

Thank you very much. I am impressed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.