简体   繁体   中英

Converting XML file into CSV with xsl

I have a Groovy script, proceeding some querires in system and as a result I get an XML file, but for the further work I need it to be transformed into csv. Unfortunately I'm compleete nubie with the xsl, and I've tried to apply different found examples and they are all performing the result, not acceptable.

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<report title="Indexing Metrics">
    <header>
        <column type="text">metrics</column>
        <column type="text">value</column>
    </header>
    <rowset>
        <row>
            <cell>Start Time</cell>
            <cell />
        </row>
        <row>
            <cell>Sucessful Requests</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Failed Requests</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Pending Requests</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Warning Requests</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Cancelled Requests</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Added Docs</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Deleted Docs</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Updated Docs</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Indexed Content Size(byte)</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Token Size(byte)</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Average Latency(seconds)</cell>
            <cell />
        </row>
        <row>
            <cell>Max Latency(seconds)</cell>
            <cell />
        </row>
        <row>
            <cell>Documents Indexed Per Second</cell>
            <cell>0</cell>
        </row>
        <row>
            <cell>Bytes Indexed Per Second</cell>
            <cell>0</cell>
        </row>
    </rowset>
</report>

And I just need all the cells divided with comma, nothing special. Please help!

You can try using the following XSLT.

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

    <xsl:template match="report">
        <xsl:for-each select="header/column">
            <xsl:value-of select="." />
            <xsl:if test="position() != last()">
                <xsl:value-of select="','" />
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="rowset/row">
            <xsl:text>&#xD;</xsl:text>
            <xsl:for-each select="cell">
                <xsl:value-of select="." />
                <xsl:if test="position() != last()">
                    <xsl:value-of select="','" />
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

This produces the following output

metrics,value
Start Time,
Sucessful Requests,0
Failed Requests,0
Pending Requests,0
Warning Requests,0
Cancelled Requests,0
Added Docs,0
Deleted Docs,0
Updated Docs,0
Indexed Content Size(byte),0
Token Size(byte),0
Average Latency(seconds),
Max Latency(seconds),
Documents Indexed Per Second,0
Bytes Indexed Per Second,0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM