Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 14th, 2005, 04:31 AM
Registered User
Join Date: Sep 2005
Location: , , .
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:
        <COL_NAME name='Name'/>
        <COL_OFFER_PRICE name='Offer'/>
        <COL_CODE name='Code'/>
        <COL_BID_PRICE name='Bid Price'/>
        <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_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>

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.


Reply With Quote
  #2 (permalink)  
Old September 16th, 2005, 03:52 PM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman


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 match="COLUMNS">
        <xsl:for-each select="child::node()">
            <xsl:value-of select="@name"/>
            <xsl:if test="position()!=last()">
            <xsl:if test="position()=last()">
    <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:template name="addRow">
        <xsl:param name="curPos"/>
        <xsl:param name="maxPos"/>
        <xsl:if test="$maxPos >= $curPos">
            <xsl:value-of select="COL_NAME"/>
            <xsl:value-of select="./COL_OFFER_PRICE/PRICE[$curPos]"/>
            <xsl:value-of select="./COL_CODE"/>
            <xsl:value-of select="./COL_BID_PRICE/PRICE[$curPos]"/>
            <xsl:call-template name="addRow">
                <xsl:with-param name="curPos" select="$curPos + 1"/>
                <xsl:with-param name="maxPos" select="$maxPos"/>


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.
Reply With Quote
  #3 (permalink)  
Old September 19th, 2005, 03:11 AM
Registered User
Join Date: Sep 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Excellent - that will keep me busy for a while.

Thanks Rushman!


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 11:33 PM.

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