I have been tasked with testing some XSLT code to ensure that it is outputting what we need. The problem is that the column data appears, but the actual data itself disappears. Since I have never dealt with XSLT or csv files before, I am at a loss of why this is happening. Here is XSLT:
public const string XSLT = @"
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""
xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"">
<xsl:output method=""text"" encoding=""us-ascii"" />
<xsl:template match=""DataRow"">IncidentNumber,IncidentDate,LocationCode,Location,DateReported,DaysFromWork,OSHAReportable, TotalDaysFromWork,InjuredEmployeeName,EmployeeType,ReportedBy,Witness,ThisIncidentIs,InjuryOrIllnessType,Hospital, Recurring,CauseOfInjury,InjuryType,BodyPartInjured,BodyFluid,AECIPremises,
<xsl:apply-templates select=""Derate""/>
</xsl:template>
<xsl:template match=""Data"">
<xsl:for-each select=""*"">
<xsl:if test=""position() != last()"">
<xsl:text>'</xsl:text>
<xsl:value-of disable-output-escaping=""yes"" select="".""/>
<xsl:text>'</xsl:text>
<xsl:value-of select=""','""/>
</xsl:if>
<xsl:if test=""position() = last()"">
<xsl:text>'</xsl:text>
<xsl:value-of disable-output-escaping=""yes"" select="".""/>
<xsl:text>'</xsl:text>
</xsl:if>
</xsl:for-each>,
</xsl:template>
</xsl:stylesheet>
";
Here is my test method:
[TestMethod]
[DeploymentItem("app.config")]
public void OneDayTest()
{
var target = new Harvester();
var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
{ ExeConfigFilename = @"app.config" }, ConfigurationUserLevel.None);
target.ConfigureHarvester(config);
var results = target.Harvest(new HarvestTargetTimeRangeUTC{StartTimeUTC = new DateTime(2012, 1, 1).ToUniversalTime(),
EndTimeUTC = DateTime.Now.ToUniversalTime()});
XmlSchemaSet set = new XmlSchemaSet();
set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) => { }));
bool valid = true;
(new XDocument(results)).Validate(set, (o, e) =>
{
valid = false;
});
Assert.IsTrue(valid);
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(XElement.Parse(Constants.XSLT).CreateReader());
var stream = new MemoryStream();
transformer.Transform(results.CreateReader(), null, new StreamWriter(stream));
stream.Position = 0;
File.WriteAllBytes("UnsubmittedWorkmansCompIncidentsReportID.csv", stream.ToArray());
}
In both my XSLT and my test method I am using a predefined company format. really all I did was tweak it to fit my data. it works in the other ones with no problem so I’m not sure why it’s not working here.
When I debug it, the data is present at this line:
transformer.Transform(results.CreateReader(), null, new StreamWriter(stream));
but after that I don’t know what happens.
If anyone could share some light on this I would appreciate it.
The problem was a mismatch in my template which I failed to notice the first 1000 times I looked at it. After walking away for a bit, coming back and reading Dimitre Novatchev comments I found the problem.