Solved

Formatting Date as YYYYMMDD in XSLT

Posted on 2006-10-25
6
1,824 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS display:none AND display:block on same element 3 77
XSLT Help 12 28
Adjust the position 3 47
Help with Syntax 9 26
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

911 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

18 Experts available now in Live!

Get 1:1 Help Now