Tech NovoGeek

...Technology Simplified

Showing posts with label XSLT. Show all posts

Thursday, October 11, 2012

How to display current Date and Time calling Templates in XSLT using C#

No comments :
The below script converts 'dd-mm-yyyy' to 'yyyymmdd' format.
<xsl:template name="formatDate">
<xsl:param name="dateTime" />
<xsl:variable name="dd" select="substring($dateTime,1,2)" />
<xsl:variable name="mm" select="substring($dateTime,4,2)" />
<xsl:variable name="yyyy" select="substring($dateTime,7,4)" />
<xsl:value-of select="concat($yyyy,$mm,$dd)" />
</xsl:template>


<xsl:call-template name="formatDate">
<xsl:with-param name="dateTime" select="TestDate" />
</xsl:call-template>

Wednesday, July 11, 2012

XSLT transformation using Windows Service

No comments :

private string ArchivePath = @"c:\Archive\";
private string InputPath = @"c:\InputFiles\";
private string OutputPath = @"c:\OutputFiles\";
To write the exception entries to Event Viewer

protected override void OnStart(string[] args)
{
Transformation();
try
{
// Uncomment this line to debug...
//System.Diagnostics.Debugger.Break();

_thread = new System.Threading.Thread(Transformation);
_thread.Start();
EventLog.WriteEntry("Successful start.", EventLogEntryType.Information);
}
catch (Exception ex)
{
// Log the exception.
EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
}


private void Transformation()
{
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
// Source paths
string xsltFilePath = "D:\\Transformation.xslt";
// refer link http://technovogeek.blogspot.ie/2012/07/how-to-display-current-date-and-time-in.html for using sample XSLT
// Compile the style sheet.
XsltSettings xslt_settings = new XsltSettings();
xslt_settings.EnableScript = true;
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltFilePath, xslt_settings, new XmlUrlResolver());
try
{
string[] inputfileEntries = Directory.GetFiles(InputPath);
foreach (string fileName in inputfileEntries)
{
// Load the XML source file.
XPathDocument doc = new XPathDocument(fileName);

// Create an XmlWriter.
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

if (!System.IO.Directory.Exists(OutputPath))
System.IO.Directory.CreateDirectory(OutputPath);
string file = System.IO.Path.Combine(OutputPath, System.IO.Path.GetFileNameWithoutExtension(fileName) + "_OUTPUT.xml");

XmlWriter writer = XmlWriter.Create(file, settings);

if (!System.IO.Directory.Exists(ArchivePath))
System.IO.Directory.CreateDirectory(ArchivePath);
string destFile = System.IO.Path.Combine(ArchivePath, System.IO.Path.GetFileName(fileName));
System.IO.File.Copy(fileName, destFile, true);
// Execute the transformation.
xslt.Transform(doc, writer);
writer.Close();
System.IO.File.Delete(fileName);
}
}
catch (Exception ex)
{
string str = "Unable to process the Conversion ";
}
}

How to display current Date and Time in XSLT using C#

No comments :
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my="put-your-namespace-uri-here"
exclude-result-prefixes="msxsl my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<msxsl:script language="C#" implements-prefix="my">
public string NowDate(){
return DateTime.Now.ToString("ddMMyyyy");
}
public string NowTime(){
return DateTime.Now.ToString("hhmmss");
}
</msxsl:script>
<xsl:template match="/">
<xsl:value-of select="my:NowDate()" />
<xsl:value-of select="my:NowTime()" />
</xsl:template>
</xsl:stylesheet>