views:

333

answers:

3

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!

+3  A: 

The creation/modification date must be written into the XML file, otherwise you cannot find it out, unless you communicate somehow with the filesystem.

This question is somewhat related: xslt-how-to-get-file-names-from-a-certain-directory

Kaarel
+3  A: 

The only things that XSLT has access to are nodes of the source tree, nodes in documents read in via the document() function, nodes in the XSLT template itself (again via the document() function), and values passed in to the transform as arguments.

So if you want the filename and its creation/modification date to be available to your transform, you have to put them in one of those places.

It's possible to implement XSLT extension methods to do this, depending on what platform you're using, but that would be my last choice.

Robert Rossney
+2  A: 

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"/>
e-holder