Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting Date as YYYYMMDD in XSLT

Posted on 2006-10-25
6
Medium Priority
?
2,009 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to count occurrences of each item in an array.

715 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