xmlxsltxslt-1.0datecreated

In XSLT, how can you get the file creation/modification date of the XML file?


I would like to know the file creation/modification date of the XML file currently being processed by my XSLT code.

I am processing an XML file and producing an HTML report. I'd like to include the date of the source XML file in the HTML report.

Note: I am using C# .NET 2008 and using the built-in XslCompiledTransform class. I have since found a solution (separate answer) using input from other answers here. Thanks!


Solution

  • After suggestions from Kaarel and Robert, I was able to reach the following solution:

    Get the file modification date in C# and pass it to the XSLT processor as follows:

    XmlTextWriter tw = new XmlTextWriter(htmlPath, null);
    tw.Formatting = Formatting.Indented;
    tw.Indentation = 4;
    
    XsltArgumentList args = new XsltArgumentList();
    FileInfo fi = new FileInfo(xmlPath);
    args.AddParam("FileDate", string.Empty,
       fi.LastWriteTime.Date.ToShortDateString());
    
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(xsltPath);
    xslt.Transform(xmlPath, args, tw);
    tw.Close();
    

    Then in the XSLT code, define and access that argument as a param as follows:

    <xsl:param name="FileDate"/>
    
    <xsl:text>Revision Date: </xsl:text>
    <xsl:value-of select="$FileDate"/>