XML to CSV using XSLT with missing elements

时光毁灭记忆、已成空白 提交于 2021-01-29 01:35:52

问题


I need to convert XML into CSV using an XSLT (compatible with .NET framework). For the moment I came up with the following solution:

XML:

<items>
    <item>
        <col1>AAA1</col1>
        <col2>BBB1</col2>
        <col3>CCC1</col3>
        <col4>DDD1</col4>
        <col5>EEE1</col5>
    </item>
    <item>
        <col1>AAA2</col1>
        <col2>BBB2</col2>
        <col5>EEE2</col5>
    </item>
    <item>
        <col1>AAA3</col1>
        <col2>BBB3</col2>
        <col3>CCC3</col3>
        <col4>DDD3</col4>
        <col5>EEE3</col5>
    </item>
</items>

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:csv="csv:csv"
    xmlns:ext="http://exslt.org/common">

    <xsl:output encoding="utf-8" method="text"/>
    <xsl:variable name="delimiter" select="';'"/>
    <xsl:variable name="newline" select="'&#xa;'"/>

    <xsl:variable name="csv_header">
        <csv:columns xmlns:csv="csv:csv">
            <column>HDR1</column>
            <column>HDR2</column>
            <column>HDR3</column>
            <column>HDR4</column>
            <column>HDR5</column>
        </csv:columns>
    </xsl:variable>

    <xsl:variable name="header" select="ext:node-set($csv_header)/*"/>

    <xsl:template name="print_value">
        <xsl:value-of select="."/>
        <xsl:if test="position() != last()">
            <xsl:value-of select="$delimiter"/>
        </xsl:if>
    </xsl:template>

    <xsl:template name="gen-header">
        <xsl:for-each select="$header/child::*" xmlns:csv="csv:csv">
            <xsl:call-template name="print_value"/>
        </xsl:for-each>
        <xsl:value-of select="$newline"/>
    </xsl:template>

    <xsl:template name="parse-node">
        <xsl:for-each select=".//child::*">
            <xsl:if test="count(*) = 0">
                <xsl:call-template name="print_value"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="parse-data">
        <xsl:for-each select="*">
            <xsl:call-template name="parse-node"/>
            <xsl:value-of select="$newline"/>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="*">
        <xsl:call-template name="gen-header"/>
        <xsl:call-template name="parse-data"/>
    </xsl:template>
</xsl:stylesheet>

Unfortunately, some tags can be missing in the input XML file (and I don't have the control over the input).

So the output CSV currently looks like this:

HDR1;HDR2;HDR3;HDR4;HDR5
AAA1;BBB1;CCC1;DDD1;EEE1
AAA2;BBB2;EEE2
AAA3;BBB3;CCC3;DDD3;EEE3

But what I need is to be able to produce the CSV like the following:

HDR1;HDR2;HDR3;HDR4;HDR5
AAA1;BBB1;CCC1;DDD1;EEE1
AAA2;BBB2;;;EEE2
AAA3;BBB3;CCC3;DDD3;EEE3

How to do that?


回答1:


If you know the columns names, then you could do simply:

XSLT 1.0

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

<xsl:template match="/items">
    <xsl:text>HDR1;HDR2;HDR3;HDR4;HDR5&#10;</xsl:text>
    <xsl:for-each select="item">
        <xsl:value-of select="col1"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="col2"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="col3"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="col4"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="col5"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/32329277/xml-to-csv-using-xslt-with-missing-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!