Hello to all. About week age I asked the question here and got a great help. Now my question has continuation. Originally I had to convert XML into text file. Here's a sample XML file:
<DOC xsi:noNamespaceSchemaLocation="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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>
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
This XSLT was posted for me and it works just great. I really appreciate it.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="' '"/>
</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>
Here is my question: The produced output file later will be load into Data Base. The table's columns correspond to the xml file attributes. Some columns allowed Null. It means some attributes could be omitted. For example if attr. DOC_NO, Weekday, SettleDate, RecNo not present the output record must be looks like this:
2009-04-23,,2009-04-23,,ZXC0000,Firm Name,1234567899,FirmId,USD,ABCD,Department Name1,,QAZ,SecName1,dc,5,ABC00000,,111,15:15:16,10,50,500
In other words I need to check for existence of each attribute. I try to solve it but my xslt knowledge very limited. Please help me. Thanks.