Skip to content

I need some XSL code to demo how to process an XML file and generate a CSV file.?

I have control over the format of the XML file and I am writing the XSL file as well.
I also want to process the XML with XSL and send the output from the XSLT processor to a SQL Server table.

One Comment

  1. Barry Anderson wrote:

    Taking the following xml:

    <Inventory>
    <Line>
    <ItemNumber>line</ItemNumber>
    <Description>desc</Description>
    <Quantity>quan</Quantity>
    <Date>date</Date>
    </Line>
    <Line>
    <ItemNumber>1</ItemNumber>
    <Description>Oak chairs</Description>
    <Quantity>6</Quantity>
    <Date>31 Dec 2004</Date>
    </Line>
    <Line>
    <ItemNumber>2</ItemNumber>
    <Description>Dining tables</Description>
    <Quantity>1</Quantity>
    <Date>31 Dec 2004</Date>
    </Line>
    </Inventory>

    A CSV file can be created with the following xsl:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="Inventory">
    <xsl:apply-templates select="Line"/>
    </xsl:template>

    <xsl:template match="Line">
    <xsl:for-each select="*">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">
    <xsl:value-of select="’,'"/>
    </xsl:if>
    </xsl:for-each>
    <xsl:text>
    </xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    The following CSV output will be given:

    line,desc,quan,date
    1,Oak chairs,6,31 Dec 2004
    2,Dining tables,1,31 Dec 2004

    Hope this helps!

    Saturday, February 20, 2010 at 1:23 pm | Permalink

Post a Comment

Your email is never published nor shared.