使用缺少元素的 XSLT 将 XML 转换为 CSV [英] XML to CSV using XSLT with missing elements

查看:28
本文介绍了使用缺少元素的 XSLT 将 XML 转换为 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 XSLT(与 .NET 框架兼容)将 XML 转换为 CSV.目前我想出了以下解决方案:

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>

遗憾的是,输入 XML 文件中可能缺少某些标签(而且我无法控制输入).

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

所以输出 CSV 目前看起来像这样:

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

但我需要的是能够生成如下所示的 CSV:

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

怎么做?

推荐答案

如果你知道列名,那么你可以简单地做:

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>

这篇关于使用缺少元素的 XSLT 将 XML 转换为 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆