Solved

Formatting Date as YYYYMMDD in XSLT

Posted on 2006-10-25
6
1,810 Views
Last Modified: 2013-11-18
Hello all,

I have a small issue.

I was given the task of taking an existing XML feed and turning it into a version of that same XML feed with certain data filtered out, so I decided to create an XSLT to do the job. Works beautifully, but the problem is that the filter criteria is as such:

<xsl:if test="myDate &lt;= '20061025'">
 .... include nodes ....
</xsl:if>

The 20061025 is a string, but because its YYYYMMDD format it seems I can get away with a string comparison because string compares are done left to right.

So the only trick is ... how can I get the XSL to take the current date instead of my hard-coded value?

Ideally I am looking for something like this:

<xsl:if test="myDate &lt;= formatDate(getCurrenDate(),'YYYYMMDD')">
 .... include nodes ....
</xsl:if>

the key though is that myDate is a text value (not a date object), so I am just looking for the string of 20061025 to compare it to.

Extra credit if you can give me CurrentDate + 1 (day) as well.

I think I saw that this can be done using a template, but was hoping that there was an easier way.

My other option is to create a 2 or 3 step process to dynamically generate this XSL file every day from a database query that returns nothing other than the formatted date string ... which seems a very unelegant solution.

Thanks in advance!
0
Comment
Question by:enforge
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17806519
Hi enforge,

There are two ways to get the current date in XSLT

1. use an extension function, but this depends on your processor
- msxml takes jscript and C#
- xalan and saxon take java

2. pass the date in the XSLT as a parameter
make currentDate a global parameter
and use the outside process to give this parameter a value

your database solution is unnecessary clumsy

if you tell us how you run the XSLT, I can help you further


Cheers!
0
 
LVL 1

Author Comment

by:enforge
ID: 17806774
Thanks Gertone.

I agree the database solution would be a clumsy one and I was trying to avoid it, but priority 1 is to get it working with priority 2 being to get it working elegantly.

I'm using the MS .NET framework's XslTransform object, and thus the MSXML parser.

If there is an easy way to slap C# into the equation I would be right in my comfort zone. Would this be in a CDATA tag?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17806847
here is a code snippet that does something similar with Jscript for msxml

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:myjs="urn:internal:my-javascript">
      <msxsl:script language="JScript" implements-prefix="myjs">
      <![CDATA[
            function GetCurrentDateTime()
            {
            var currentTime = new Date();
            var month = currentTime.getMonth() + 1;
            var day = currentTime.getDate();
            var year = currentTime.getFullYear();
            return(month + "/" + day + "/" + year);
            }
      ]]>
      </msxsl:script>

<xsl:template match="/">
      <xsl:value-of select="myjs:GetCurrentDateTime()"/>
</xsl:template>

</xsl:stylesheet>

you need to define this namespace xmlns:msxsl="urn:schemas-microsoft-com:xslt"
in order for the script tag to be known
and you define a random namespace, I choose xmlns:myjs="urn:internal:my-javascript"
for the implementation of the function
Inside the script tag you stuff the code in a CDATA section, indeed

In the template match="/" I show how to call this new function

C# works similarly

cheers

Geert

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 125 total points
ID: 17806872
this site
http://msdn2.microsoft.com/en-us/library/533texsx.aspx

gives us this C# example

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts">

  <msxsl:script language="C#" implements-prefix="user">
     <![CDATA[
     public double circumference(double radius){
       double pi = 3.14;
       double circ = pi*radius*2;
       return circ;
     }
      ]]>
   </msxsl:script>

  <xsl:template match="data">  
  <circles>

  <xsl:for-each select="circle">
    <circle>
    <xsl:copy-of select="node()"/>
       <circumference>
          <xsl:value-of select="user:circumference(radius)"/>
       </circumference>
    </circle>
  </xsl:for-each>
  </circles>
  </xsl:template>
</xsl:stylesheet>
0
 
LVL 1

Author Comment

by:enforge
ID: 17807227
Thanks Gertone, this worked to perfection!!! Exactly what I was looking for.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17809900
welcome
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

743 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

14 Experts available now in Live!

Get 1:1 Help Now