Solved

Formatting Date as YYYYMMDD in XSLT

Posted on 2006-10-25
6
1,861 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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 …
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. 
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

821 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