0

Sample Xml file;

<?xml version="1.0"?>
<LevelSet displayName="Sides">
<Level uniqueID="0" PayReceive="Pay" CommodityCode="" CommodityName="GAS" LocationCode="" NearbyDescription="First">
    <SubLevelSets>
        <SubLevelSet displayName="Discreet Periods">
            <SubLevel uniqueID="1" PD="1" StartDate="20160101" EndDate="20160131" PaymentDate="20160222" DenominationCCY="USD" DiscountFactor="0.999618" ClientPV="-314639.64" />
            <SubLevel uniqueID="2" PD="2" StartDate="20160201" EndDate="20160229" PaymentDate="20160321" DenominationCCY="USD" DiscountFactor="0.999182" ClientPV="-263423.84" />
        </SubLevelSet>
    </SubLevelSets>
</Level>
<Level uniqueID="1" PayReceive="Receive" CommodityCode="" CommodityName="GAS" LocationCode="" NearbyDescription="First">
    <SubLevelSets>
        <SubLevelSet displayName="Discreet Periods">
            <SubLevel uniqueID="1" PD="1" StartDate="20160101" EndDate="20160131" PaymentDate="20160222" DenominationCCY="USD" DiscountFactor="0.999618" ClientPV="871990.82" />
            <SubLevel uniqueID="2" PD="2" StartDate="20160201" EndDate="20160229" PaymentDate="20160321" DenominationCCY="USD" DiscountFactor="0.999182" ClientPV="832316.89" />
        </SubLevelSet>
    </SubLevelSets>
</Level>
</LevelSet>

Expected output;

PayReceive,CommodityName,StartDate,ClientPV
Pay,GAS,20160101,-314639.64
Pay,GAS,20160201,-263423.84
Receive,GAS,20160101,871990.82
Receive,GAS,20160201,832316.89  

Below is the xsl I am using

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
PayReceive,CommodityName,StartDate,ClientPV
<xsl:for-each select="//Level">
<xsl:value-of select="@PayReceive"/><xsl:text>,</xsl:text>
<xsl:value-of select="@CommodityName" /><xsl:text>,</xsl:text>
<xsl:for-each select="//SubLevel">
<xsl:value-of select="@StartDate"/><xsl:text>,</xsl:text>
<xsl:value-of select="@ClientPV"/>
<xsl:text>&#xA;</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Daniel
  • 42,087
  • 4
  • 55
  • 81
Amit Singh
  • 63
  • 2
  • 12
  • Possible duplicate of [XML to CSV Using XSLT](http://stackoverflow.com/questions/365312/xml-to-csv-using-xslt) – zx485 May 06 '16 at 07:35

2 Answers2

0

You can use these two templates :

<xsl:template match="/">
    <xsl:text>PayReceive,CommodityName,StartDate,ClientPV</xsl:text>
    <xsl:text>&#xA;</xsl:text>
    <xsl:apply-templates select="*/Level"/>
</xsl:template>

<xsl:template match="Level">
    <xsl:variable name="PayReceive" select="@PayReceive"/>
    <xsl:variable name="CommodityName" select="@CommodityName"/>
    <xsl:for-each select="*/*/SubLevel">
        <xsl:value-of select="$PayReceive"/><xsl:text>,</xsl:text>
        <xsl:value-of select="$CommodityName" /><xsl:text>,</xsl:text>
        <xsl:value-of select="@StartDate"/><xsl:text>,</xsl:text>
        <xsl:value-of select="@ClientPV"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template>
har07
  • 88,338
  • 12
  • 84
  • 137
0

With some changes to your stylesheet it will do:

Because there should be a line in output for each SubLevel, iterate first to SubLevel. Access additional information from SubLevel with ancestor::Level. Try:

<xsl:stylesheet version="1.0" 
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                   >
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="/">
      <xsl:text>PayReceive,CommodityName,StartDate,ClientPV</xsl:text>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="//SubLevel">
        <xsl:value-of select="ancestor::Level/@PayReceive"/><xsl:text>,</xsl:text>
        <xsl:value-of select="ancestor::Level/@CommodityName" /><xsl:text>,</xsl:text>

        <xsl:value-of select="@StartDate"/><xsl:text>,</xsl:text>
        <xsl:value-of select="@ClientPV"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:template>
  </xsl:stylesheet>
hr_117
  • 9,589
  • 1
  • 18
  • 23