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 December 29th, 2007, 10:06 AM
Registered User
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default translating XML into CSV file

Hello,

I have an XML file of the following format
<root>
        <C value=1>
        <C value=2>
        â€¦
        <C value=N>

        <parent>
                <A name=1>
                        <B value=1>
                        <B value=2>
                        â€¦

                        <B value=J>

                </A name=1>
                <A name=2>
                        <B value=1>
                        <B value=2>
                        â€¦

                        <B value=J>

                </A name=2>
                â€¦
                <A name=K>
                        <B value=1>
                        <B value=2>
                        â€¦

                        <B value=J>

                </A name=K>
        </parent >
</root>

to be translated into CSV of the following format:

Val(C1),Val(C2),...,Val(CN),Val(B1 of A 1),Val(B1 of A2),...,Val(B1 of A K)
Val(C1),Val(C2),...,Val(CN),Val(B2 of A 1),Val(B2 of A2),...,Val(B2 of A K)
.................................................. .................
Val(C1),Val(C2),...,Val(CN),Val(BJ of A 1),Val(BJ of A 2),...,Val (BJ of A K)

I am out of ideas, but since I am very new to XSLT my ideas pool is limited on the subject. Any help is greatly appreciated. Thanks.
 
Old December 29th, 2007, 12:01 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

So you want one line in the output for each B element and each line has all C/@value values and then the B/@value values of the same position:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="text"/>

  <xsl:param name="ls" select="'#13;#10;'"/>

  <xsl:param name="is" select="','"/>

  <xsl:variable name="c-values" select="/root/C/@value"/>

  <xsl:template match="/">
    <xsl:apply-templates select="root/parent/A[1]/B"/>
  </xsl:template>

  <xsl:template match="B">
    <xsl:variable name="pos" select="position()"/>
    <xsl:apply-templates select="$c-values | ./@value | ../../A/B[position() = $pos]/@value"/>
    <xsl:value-of select="$ls"/>
  </xsl:template>

  <xsl:template match="@value">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">
      <xsl:value-of select="$is"/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
With the input corrected to look like this:
Code:
<root>
        <C value="1"></C>
        <C value="2"></C>
        â€¦
        <C value="N"></C>

        <parent>
                <A name="1">
                        <B value="1"></B>
                        <B value="2"></B>
                        â€¦

                        <B value="J"></B>

                </A>
                <A name="2">
                        <B value="1"></B>
                        <B value="2"></B>
                        â€¦

                        <B value="J"></B>

                </A>
                â€¦
                <A name="K">
                        <B value="1"></B>
                        <B value="2"></B>
                        â€¦

                        <B value="J"></B>

                </A>
        </parent>
</root>
the result is the following:
Code:
1,2,N,1,1,1
1,2,N,2,2,2
1,2,N,J,J,J





Similar Threads
Thread Thread Starter Forum Replies Last Post
transform csv file to xml David Li XSLT 8 December 11th, 2012 04:57 AM
.csv to xml conversion balakrishna XML 0 March 5th, 2007 01:07 PM
XML to CSV reloader2 XML 2 September 7th, 2005 08:51 AM
translating a flat xml to a hierarchical xml stevea XSLT 4 June 13th, 2005 05:55 PM
Translating a XML with XSL and saving results DevOne XML 1 April 9th, 2005 07:32 AM





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