Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2005, 04:31 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested XML to CSV transformation

Hi All,
Apologies for yet another XML to CSV post - I see there are a lot of similar topics already but I havent seen any that would quite cover what I need to do.

This is a simplified snippet of my XML:
<REPORT>
    <COLUMNS>
        <COL_NAME name='Name'/>
        <COL_OFFER_PRICE name='Offer'/>
        <COL_CODE name='Code'/>
        <COL_BID_PRICE name='Bid Price'/>
    </COLUMNS>
    <DATA_ROW>
        <COL_NAME>Growth Fund</COL_NAME>
        <COL_OFFER_PRICE nested='True'>
            <PRICE itemDate='2001-01-01'>1.00</PRICE>
            <PRICE itemDate='2001-01-02'>1.01</PRICE>
            <PRICE itemDate='2001-01-03'>1.04</PRICE>
            ...........................
        </COL_OFFER_PRICE>
        <COL_CODE>12321</COL_CODE>
        <COL_BID_PRICE nested='True'>
            <PRICE itemDate='2001-01-01'>1.01</PRICE>
            <PRICE itemDate='2001-01-02'>1.02</PRICE>
            <PRICE itemDate='2001-01-03'>1.09</PRICE>
            ...........................
        </COL_BID_PRICE>
    </DATA_ROW>
    <DATA_ROW>
        ...........................
    </DATA_ROW>
</REPORT>

It represents a report, each <COLUMNS> item is a column heading & each <DATA_ROW> item is a row of data. There may be one or many <DATA_ROW> items.

Each <DATA_ROW> represents a row of data about a fund in this context.
Items within the <DATA_ROW> may be 'static data' which is not date sensitive such as 'fund name' or they may have a 'nested' attribute which means that they have sub-rows of data.

I need to transform the XML into a CSV file like:

Name, Offer, Code, Bid
Growth Fund, 1.00, 12321, 1.01
Growth Fund, 1.01, 12321, 1.02
Growth Fund, 1.04, 12321, 1.09

So for each nested item within a data row, I need to repeat the 'static' data for all of its children.
All nested items within a <DATA_ROW> will have the same amount of children.

Confused? I am...
Any help/guidance at all gratefully accepted.

Regards,
Patrick.


 
Old September 16th, 2005, 03:52 PM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Here!

Try it with a complete exemple. Hope it helps!

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//COLUMNS"></xsl:apply-templates>
        <xsl:apply-templates select="//DATA_ROW"></xsl:apply-templates>
    </xsl:template>
    <xsl:template match="COLUMNS">
        <xsl:for-each select="child::node()">
            <xsl:value-of select="@name"/>
            <xsl:if test="position()!=last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:if test="position()=last()">
                <xsl:text>#x0d;#x0a;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="DATA_ROW">
        <xsl:variable name="nbNested" select="count(./COL_OFFER_PRICE//PRICE)"/>
        <xsl:call-template name="addRow">
            <xsl:with-param name="curPos" select="1"/>
            <xsl:with-param name="maxPos" select="$nbNested"/>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="addRow">
        <xsl:param name="curPos"/>
        <xsl:param name="maxPos"/>
        <xsl:if test="$maxPos >= $curPos">
            <xsl:value-of select="COL_NAME"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="./COL_OFFER_PRICE/PRICE[$curPos]"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="./COL_CODE"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="./COL_BID_PRICE/PRICE[$curPos]"/>
            <xsl:text>#x0d;#x0a;</xsl:text>
            <xsl:call-template name="addRow">
                <xsl:with-param name="curPos" select="$curPos + 1"/>
                <xsl:with-param name="maxPos" select="$maxPos"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Rushman

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.
 
Old September 19th, 2005, 03:11 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent - that will keep me busy for a while.

Thanks Rushman!

-Patrick.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL transformation - PIPE delimited to CSV satish9 XSLT 3 June 11th, 2007 10:21 AM
transformation from attributes to nested elements e-bell XSLT 2 January 21st, 2007 07:21 PM
Is XML supports transformation of HTML to XML? zeeonline XSLT 1 July 28th, 2006 05:13 PM
Problem in XML to XML transformation jkuravatti XSLT 2 May 5th, 2006 11:21 AM
XML to XML transformation using XSLT karjagis XSLT 3 July 30th, 2004 06:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.