Solved

Formatting Date as YYYYMMDD in XSLT

Posted on 2006-10-25
6
1,840 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 116
How do I bind the results to a grid 3 36
Randomize in Owl Carousel v1.3.2 6 43
Make Line Items Look Good Over Mobile 2 58
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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