Programming Newbie
Programming Newbie

Reputation: 1227

xslt: Removing comma at the end of line but not new line

I've been tasked to make a change on an existing xslt string. What I need to do is remove the comma at the end of a line. I found what is causing the comma, the problem is, when I make the change to remove the comma, the csv file becomes one large run-on string instead of having a record, new line, record, etc.

Here is the 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"">Incident No.,Incident Date,Location Code,Location,Date Reported,Days away from Work (H),Is the incident OSHA Reportable?,Total Days Away From Work,Name of injured employee,Employee Type,Reported By,Witness (Name and Phone #),This Incident is an,""Select """"Injury"""" or choose Illness Type: (M)"",Was employee taken to the hospital?,Was this a recurring incident?,Cause of Injury,Injury Type,Body Part Injured,Body Fluid Exposure?,Did Injury/Illness occur on AECI premises?
<xsl:apply-templates select=""Data""/>

</xsl:template>

<xsl:template match=""Data"">
  <xsl:for-each select=""*"">
    <xsl:if test=""position() != last()"">    
      <xsl:value-of disable-output-escaping=""yes"" select="".""/>    
      <xsl:value-of select=""','""/>
    </xsl:if>
    <xsl:if test=""position() = last()"">   
      <xsl:value-of disable-output-escaping=""yes"" select="".""/>                          
    </xsl:if>
  </xsl:for-each>,    <-----THIS IS WHAT IS CAUSING THE COMMA AT THE END OF THE LINE
</xsl:template>
</xsl:stylesheet>";

Here is what the csv file looks like prior to removing that comma. I have removed the names and replaced them with **

Incident No.,Incident Date,Location Code,Location,Date Reported,Days away from Work (H),Is the incident OSHA Reportable?,Total Days Away From Work,Name of injured employee,Employee Type,Reported By,Witness (Name and Phone #),This Incident is an,"Select ""Injury"" or choose Illness Type: (M)",Was employee taken to the hospital?,Was this a recurring incident?,Cause of Injury,Injury Type,Body Part Injured,Body Fluid Exposure?,Did Injury/Illness occur on AECI premises?
2858,7/24/2012,HQ,Accounting and Finance,,,No,,,,,,,Environmental,No,,,,,,No,
2852,7/23/2012,TH,TH - RESULTS/LAB,2012-07-23t00:07:00,0,No,,****, ****,Represented,****, ****,,(1) Information Only,Environmental,No,,...   Acid Chemicals  ,...   No Physical Injury,...   Facial Bones  ,,Yes,

I just want to remove the comment at the very end of the line. Here is what it looks like after I remove the comma at the end of this: ,

Incident No.,Incident Date,Location Code,Location,Date Reported,Days away from Work (H),Is the incident OSHA Reportable?,Total Days Away From Work,Name of injured employee,Employee Type,Reported By,Witness (Name and Phone #),This Incident is an,"Select ""Injury"" or choose Illness Type: (M)",Was employee taken to the hospital?,Was this a recurring incident?,Cause of Injury,Injury Type,Body Part Injured,Body Fluid Exposure?,Did Injury/Illness occur on AECI premises?
2858,7/24/2012,HQ,Accounting and Finance,,,No,,,,,,,Environmental,No,,,,,,No2852,7/23/2012,TH,TH - RESULTS/LAB,2012-07-23t00:07:00,0,No,,****, ****,Represented,****, ****,,(1) Information Only,Environmental,No,,...   Acid Chemicals  ,...   No Physical Injury,...   Facial Bones  ,,Yes2849,7/21/2012,HQ,New Madrid,,,No,,,,,,,Environmental,No,,,,,,

Upvotes: 0

Views: 605

Answers (1)

Tim C
Tim C

Reputation: 70638

When you have the comma there, the XLST processor will see this as text to be output, and so output the comma, along with the newline character after it.

Remove the comma, and there is only whitespace between the xsl elements, which is then dis-regarded by the processor and not output.

One solution is to explicitly output a new line character where the comma currently is:

<xsl:value-of select=""'&#10;'"" />

Or maybe (to do a carriage return and line feed)

<xsl:value-of select=""'&#13;&#10;'"" />

Upvotes: 1

Related Questions