xslt:删除行末但不是新行的逗号

时间:2012-07-26 12:53:07

标签: xslt

我的任务是对现有的xslt字符串进行更改。我需要做的是删除行尾的逗号。我找到了导致逗号的原因,问题是,当我进行更改以删除逗号时,csv文件变成一个大的连续字符串而不是记录,新行,记录等。

这是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>";

以下是删除该逗号之前csv文件的样子。我删除了名称并将其替换为 * *

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,

我只想删除该行末尾的评论。这是我删除逗号后面的内容:

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,,,,,,

1 个答案:

答案 0 :(得分:1)

当你有逗号时,XLST处理器会将此视为要输出的文本,因此输出逗号以及后面的换行符。

删除逗号,xsl元素之间只有空格,然后由处理器忽略而不输出。

一种解决方案是显式输出逗号当前为的新行字符:

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

或者也许(做回车和换行)

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