1

我有 xslt 将 xml 文件转换为逗号分隔的文件,但是在每条记录之后它都会创建额外的行。

我试过放,但没有用。任何可以提供帮助的帮助或链接将不胜感激。

资源:

<XML>
<Record><GroupId>2001</GroupId><Date>05-May-2012</Date><Time>10:20:38</Time><TxnId>267-2001-1-29555-2</TxnId><AgencyID>OrdinaryAccountWithdraw</AgencyID><AccTechAgencyType>Out Payment</AccTechAgencyType><AccTechAgencyAmount>30000</AccTechAgencyAmount><CustomerRef>703589491001</CustomerRef></Record>
<Record><GroupId>2001</GroupId><Date>05-May-2012</Date><Time>13:21:50</Time><TxnId>267-2001-1-29694-2</TxnId><AgencyID>SpecialAccountWithdraw</AgencyID><AccTechAgencyType>Out Payment</AccTechAgencyType><AccTechAgencyAmount>20000</AccTechAgencyAmount><CustomerRef>703677841501</CustomerRef></Record>
<Record><GroupId>2001</GroupId><Date>07-May-2012</Date><Time>10:10:08</Time><TxnId>267-2001-1-29780-2</TxnId><AgencyID>InPay_FuneralPlan</AgencyID><AccTechAgencyType>In Payment</AccTechAgencyType><AccTechAgencyAmount>5000</AccTechAgencyAmount><CustomerRef>MP007235</CustomerRef></Record>
<Record><GroupId>2001</GroupId><Date>07-May-2012</Date><Time>10:15:36</Time><TxnId>267-2001-1-29786-2</TxnId><AgencyID>SpecialAccountWithdraw</AgencyID><AccTechAgencyType>Out Payment</AccTechAgencyType><AccTechAgencyAmount>30000</AccTechAgencyAmount><CustomerRef>703690771501</CustomerRef></Record>
<Record><GroupId>2001</GroupId><Date>07-May-2012</Date><Time>10:27:42</Time><TxnId>267-2001-1-29798-3</TxnId><AgencyID>SpecialAccountDeposit</AgencyID><AccTechAgencyType>In Payment</AccTechAgencyType><AccTechAgencyAmount>50000</AccTechAgencyAmount><CustomerRef>703437751501</CustomerRef></Record>
</XML>

xslt:

<?xml version='1.0' ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt"   xmlns:user="http://mycompany.com/mynamespace"> <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
      <xsl:template name="format-date">
<xsl:param name="AGDateValue">01-Jan-2000</xsl:param>
<xsl:value-of select="substring($AGDateValue, 8, 4)"/>
<xsl:choose>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Jan'">01</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Feb'">02</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Mar'">03</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Apr'">04</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'May'">05</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Jun'">06</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Jul'">07</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Aug'">08</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Sep'">09</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Oct'">10</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Nov'">11</xsl:when>
    <xsl:when test="substring($AGDateValue, 4, 3) = 'Dec'">12</xsl:when>
    <xsl:otherwise>00</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="substring($AGDateValue, 1, 2)"/>
</xsl:template>
<xsl:template name="format-time">
<xsl:param name="AGTimeValue">12:12:12</xsl:param>
<xsl:text>T</xsl:text>
<xsl:value-of select="substring($AGTimeValue, 1, 2)"/>
<xsl:value-of select="substring($AGTimeValue, 4, 2)"/>
</xsl:template>
<xsl:template match="/">
<xsl:for-each select="/XML/Record">
      <xsl:value-of select="GroupId" />      
      <xsl:text>|</xsl:text>
      <xsl:call-template name="format-date"><xsl:with-param name="AGDateValue" select="Date" /></xsl:call-template>
      <xsl:call-template name="format-time"><xsl:with-param name="AGTimeValue" select="Time" /></xsl:call-template>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="AgencyID" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="AccTechAgencyType" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="CustomerRef" />
      <xsl:text>|</xsl:text>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="TxnId" />
      <xsl:text>|</xsl:text>
      <xsl:value-of select="format-number(AccTechAgencyAmount*0.01,'0.00')" />
  <xsl:text>&#xD;</xsl:text>         
</xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

输出:

2001|20120505T1020|OrdinaryAccountWithdraw|Out Payment|703589||267-2001-1-29555-2|300.00

2001|20120505T1321|SpecialAccountWithdraw|Out Payment|703677||267-2001-1-29694-2|200.00

2001|20120507T1010|InPay_FuneralPlan|In Payment|MP00723||267-2001-1-29780-2|50.00

2001|20120507T1015|SpecialAccountWithdraw|Out Payment|703690||267-2001-1-29786-2|300.00

2001|20120507T1027|SpecialAccountDeposit|In Payment|7034377||267-2001-1-29798-3|500.00
4

2 回答 2

0

试试这个更简洁的版本......

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<!-- Configure $new-line below as your require for your text file format type. -->
<xsl:variable name="new-line" select="'&#x0A;'" /> 
<!-- An alternative configuration is '&#x0D;&#x0A;' --> 

<xsl:template name="format-date">
 <xsl:param name="AGDateValue" select="'01-Jan-2000'" />
 <xsl:value-of select="substring($AGDateValue, 8, 4)"/>
  <xsl:variable name="month" select="substring($AGDateValue, 4, 3)" />
  <xsl:value-of select="format-number(
          translate( $month, 'nFrylgSONDJaebMApupctov', '00240107666') +
          translate( $month, 'aFpugONDJnebMrAyluSctovc', '12268456'), '00')" />
   <xsl:value-of select="substring($AGDateValue, 1, 2)"/>
</xsl:template>

<xsl:template name="format-time">
 <xsl:param name="AGTimeValue" select="'12:12:12'" />
 <xsl:text>T</xsl:text>
 <xsl:value-of select="substring($AGTimeValue, 1, 2)" />
 <xsl:value-of select="substring($AGTimeValue, 4, 2)" />
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates select="/XML/Record"/>
</xsl:template>

<xsl:template match="Record">
  <xsl:value-of select="concat(GroupId,'|')" />      
  <xsl:call-template name="format-date"><xsl:with-param name="AGDateValue" select="Date" /></xsl:call-template>
  <xsl:call-template name="format-time"><xsl:with-param name="AGTimeValue" select="Time" /></xsl:call-template>
  <xsl:value-of select="concat('|',AgencyID,'|',AccTechAgencyType,'|',CustomerRef,'||',TxnId,'|')" />
  <xsl:value-of select="format-number(AccTechAgencyAmount*0.01,'0.00')" />
  <xsl:value-of select="$new-line" />         
</xsl:template> 

</xsl:stylesheet>
于 2012-06-27T05:26:25.293 回答
0

在 XSLT 文件中添加换行符最简单,如下所示:

      <xsl:text>hello world</xsl:text>
      <xsl:text>
</xsl:text>

测试

我对上面的测试数据进行了转换:

xsltproc convert.xsl data.xml > output.txt

转换.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace" version="1.0">

  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template name="format-date">
    <xsl:param name="AGDateValue">01-Jan-2000</xsl:param>
    <xsl:value-of select="substring($AGDateValue, 8, 4)"/>
    <xsl:choose>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Jan'">01</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Feb'">02</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Mar'">03</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Apr'">04</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'May'">05</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Jun'">06</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Jul'">07</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Aug'">08</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Sep'">09</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Oct'">10</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Nov'">11</xsl:when>
      <xsl:when test="substring($AGDateValue, 4, 3) = 'Dec'">12</xsl:when>
      <xsl:otherwise>00</xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="substring($AGDateValue, 1, 2)"/>
  </xsl:template>

  <xsl:template name="format-time">
    <xsl:param name="AGTimeValue">12:12:12</xsl:param>
    <xsl:text>T</xsl:text>
    <xsl:value-of select="substring($AGTimeValue, 1, 2)"/>
    <xsl:value-of select="substring($AGTimeValue, 4, 2)"/>
  </xsl:template>

  <xsl:template match="/">
    <xsl:for-each select="/XML/Record">
      <xsl:value-of select="GroupId"/>
      <xsl:text>|</xsl:text>
      <xsl:call-template name="format-date">
        <xsl:with-param name="AGDateValue" select="Date"/>
      </xsl:call-template>
      <xsl:call-template name="format-time">
        <xsl:with-param name="AGTimeValue" select="Time"/>
      </xsl:call-template>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="AgencyID"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="AccTechAgencyType"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="CustomerRef"/>
      <xsl:text>|</xsl:text>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="TxnId"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="format-number(AccTechAgencyAmount*0.01,'0.00')"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

输出.txt

2001|20120505T1020|OrdinaryAccountWithdraw|Out Payment|703589491001||267-2001-1-29555-2|300.00
2001|20120505T1321|SpecialAccountWithdraw|Out Payment|703677841501||267-2001-1-29694-2|200.00
2001|20120507T1010|InPay_FuneralPlan|In Payment|MP007235||267-2001-1-29780-2|50.00
2001|20120507T1015|SpecialAccountWithdraw|Out Payment|703690771501||267-2001-1-29786-2|300.00
2001|20120507T1027|SpecialAccountDeposit|In Payment|703437751501||267-2001-1-29798-3|500.00
于 2012-06-26T17:35:21.253 回答