使用xslt将xml转换为Pipe分隔的文本文件,在每条记录后添加额外的行

时间:2012-06-26 15:48:46

标签: xml xslt

我有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

2 个答案:

答案 0 :(得分:0)

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

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

测试

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

xsltproc convert.xsl data.xml > output.txt

convert.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>

output.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

答案 1 :(得分: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>