Date manipulation in XSL

Posted on 2006-05-22
Last Modified: 2010-05-18
I need to transform a source XML file and add some date processing to the output. The output needs to be another XML file, not HTML for a browser. The date functions work fine but the output is not XML -  trying to translate the XML via .Net 1.1 VB code produces the following exception message: File:///c:/<path>/DateToday.xsl(9,2):

All I get is the following string when I open DateToday.XML in IE6
Fred22/05/2006 16:22:3122/07/2006 16:22:3161

My test files are as follows:

Input file DateToday.XML
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="DateToday.xsl"?>


Translation file DateToday.XSL
<?xml version="1.0" encoding="Windows-1252" ?>
<xsl:output method="xml" encoding="utf-8" />

<xsl:include href="DateLibrary.xsl"/>

<xsl:template match="/">

<xsl:variable name="DateToday" select="extra:FormatDateToday()"/>
<xsl:variable name="CalculatedDate" select="extra:AddDate('m',2,$DateToday)"/>
<xsl:variable name="DateDifference" select="extra:DiffDate('d',$DateToday,$CalculatedDate)"/>

    <xsl:value-of select="//DateNow"/>
    <xsl:value-of select="$DateToday"/>
    <xsl:value-of select="$CalculatedDate"/>
    <xsl:value-of select="$DateDifference"/>



Date processing function file DateLibrary.XSL
<msxsl:script xmlns:msxsl="urn:schemas-microsoft-com:xslt"
Function AddDate(Interval, NumberOfIntervals, Date)
  AddDate = FormatDateTime(DateAdd(Interval, NumberOfIntervals, Date))
End Function
Function FormatDateToday()
  FormatDateToday = FormatDateTime(Now(),0)
End Function

Function FormatDateAndTime(SelectedDate, FormatType)
  FormatDateAndTime = FormatDateTime(SelectedDate, FormatType)
End Function

Function DiffDate(Interval, Date1, Date2)
  DiffDate = DateDiff(Interval, Date1, Date2)
End Function


Question by:MBL1175
    LVL 60

    Accepted Solution

    Hi MBL1175,

    your XSLT works fine
    the problem is your browser.
    The browser threats the XML result as if it were HTML
    and for that the browser ignores the unknown tags... showing you only the text content, not the tags

    if you download msxsl from the microsoft website
    you can run the following on the commandline

    msxsl DateToday.xml DateToday.xsl -o DateResult.xml

    you will see that DateResult.xml contains your expected xml


    Author Comment

    Hi Gertone

    Thanks for the update - MSXSL worked just fine.

    However, I don't understand why I don't get the same result from the transformation in my VB code. Can you shed any light on why MSXSL works but VB.Net doesn't??


    Featured Post

    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.

    Join & Write a Comment

    Suggested Solutions

    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    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. 
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    731 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

    15 Experts available now in Live!

    Get 1:1 Help Now