tags:

views:

355

answers:

1

I'm trying convert an XML file to text using XSLT, but I don't have any experience using XSLT.

Here's a sample XML file:

<DOC xsi:noNamespaceSchemaLocation="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
 <DOC_REQUISITES DOC_DATE="2009-04-23" DOC_NO="99999999"/>
 <DOCID TradeDate="2009-04-23" Weekday="Monday" MainFirmId="ZXC0000" FirmName="Firm Name" FirmINN="1234567899">
  <FIRM FirmID="FirmId">
   <CURRENCY CurrencyId="USD">
    <DEPARTMENT DepartmentId="ABCD" DepName="Department Name1">
     <SETTLEDATE SettleDate="2009-04-23">
      <SECURITY SecurityId="QAZ" SecShortName="SecName1" SecurityType="dc" FaceValue="5">
       <TRDACC TrdAccId="ABC00000">
        <RECORDS RecNo="1" TradeNo="111" TradeTime="15:15:16" Price="10" Quantity="50" Value="500"/>
       </TRDACC>
       <TRDACC TrdAccId="SDC00000">
        <RECORDS RecNo="2" TradeNo="112" TradeTime="15:15:16" Price="10" Quantity="50" Value="500"/>
        <RECORDS RecNo="3" TradeNo="113" TradeTime="15:15:16" Price="20" Quantity="10" Value="200"/>
       </TRDACC>
      </SECURITY>
      <SECURITY SecurityId="WSX" SecShortName="SecName2" SecurityType="dc" FaceValue="1">
       <TRDACC TrdAccId="ABC00000">
        <RECORDS RecNo="4" TradeNo="114" TradeTime="15:15:13" Price="2" Quantity="1" Value="2"/>
       </TRDACC>
      </SECURITY>
     </SETTLEDATE>
    </DEPARTMENT>
    <DEPARTMENT DepartmentId="CBSD" DepName="Department Name2">
     <SETTLEDATE SettleDate="2009-05-20">
      <SECURITY SecurityId="RFV" SecShortName="SecName3" SecurityType="dc" FaceValue="2">
       <TRDACC TrdAccId="SDC00000">
        <RECORDS RecNo="5" TradeNo="115" TradeTime="15:15:13" Price="100" Quantity="10" Value="1000"/>
       </TRDACC>
      </SECURITY>
     </SETTLEDATE>
    </DEPARTMENT>
   </CURRENCY>
  </FIRM>
 </DOCID>
</DOC>

My desired output looks like this:

2009-04-23,99999999,2009-04-23,Monday,ZXC0000,Firm Name,1234567899,FirmId,USD,ABCD,Department Name1,2009-04-23,QAZ,SecName1,dc,5,ABC00000,1,111,15:15:16,10,50,500
2009-04-23,99999999,2009-04-23,Monday,ZXC0000,Firm Name,1234567899,FirmId,USD,ABCD,Department Name1,2009-04-23,QAZ,SecName1,dc,5,SDC00000,2,112,15:15:16,10,50,500
2009-04-23,99999999,2009-04-23,Monday,ZXC0000,Firm Name,1234567899,FirmId,USD,ABCD,Department Name1,2009-04-23,QAZ,SecName1,dc,5,SDC00000,3,113,15:15:16,20,10,200
2009-04-23,99999999,2009-04-23,Monday,ZXC0000,Firm Name,1234567899,FirmId,USD,ABCD,Department Name1,2009-04-23,WSX,SecName2,dc,1,ABC00000,4,114,15:15:13,2,1,2
2009-04-23,99999999,2009-04-23,Monday,ZXC0000,Firm Name,1234567899,FirmId,USD,CBSD,Department Name2,2009-05-20,RFV,SecName3,dc,2,SDC00000,5,115,15:15:13,100,10,1000
+1  A: 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="/DOC/DOCID/FIRM/CURRENCY/DEPARTMENT/SETTLEDATE/SECURITY/TRDACC"/>
  </xsl:template>

<xsl:template match="TRDACC">
    <!--Select all of the attribute values from the preceding DOC_REQUISITES attibutes, TRDACC ancestor elements, the current element, and all of it's descendants and then apply-templates to those attributes -->
    <xsl:apply-templates select="preceding::DOC_REQUISITES/@* | ancestor::*[not(local-name()='DOC')]/@* | @* | descendant::*/@*"/>
    <!--Adds a carriage return at the end of the line -->
    <xsl:value-of select="'&#10;'"/>
</xsl:template>

<!--Template match for attributes that emits the attribute value and a ',', except for the last one -->
<xsl:template match="@*[.!='']">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">,</xsl:if>
</xsl:template>

</xsl:stylesheet>
Mads Hansen
`<xsl:output method="xml" indent="yes" encoding="UTF-8"/>` certainly isn't correct here.
Mark E
@Mark - was in the process of updating. output directive is set for text and to omit-xml-declaration
Mads Hansen