如何将复杂的Xml转换为csv?

阿列克谢·库德里亚夫采夫(Alexey Kudryavtsev):

我正在用Java(初中)编写程序,我确实需要有关xslt转换的帮助。有必要从xml制作一个csv文件。我得到了这个xslt过滤器:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

    <xsl:template match="node()" name="conv">
        <xsl:call-template name="loop"/>
    </xsl:template>

    <xsl:template name="loop">

        <xsl:for-each select="./*[count(*) = 0]">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
            <xsl:if test="position() = last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>


        <xsl:for-each select="./*[(count(*) != 0) and (name()!='PARAMETRS')] ">
            <xsl:call-template name="loop"/>
        </xsl:for-each>
            <xsl:text>&#xA;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

源xml:

<Integration>
    <PARAMETRS>
        <ID>AZD</ID>
        <DATE>2020-01-01</DATE>
    </PARAMETRS>
    <ORG>
        <Thing>
            <object>10220</object>
            <type>U</type>
            <dyn>
                <items>
                    <val>988009</val>
                    <datebegin>2019-12-12</datebegin>
                </items>
            </dyn>
        </Thing>
        <Thing>
            <object>10221</object>
            <type>U</type>
            <dyn>
                <items>
                    <val>988010</val>
                    <datebegin>2019-12-13</datebegin>
                </items>
                <items>
                    <val>988011</val>
                    <datebegin>2019-12-14</datebegin>
                </items>
            </dyn>
        </Thing>
    </ORG>
</Integration>

在输出中,我得到了逗号分隔的行,以及带有以下值的其他几行(相同的项目)。并且无法弄清楚如何连接值...我可以通过value-of select =“ concat”来实现,但是我可能有多个dyn(1、2、3 ...),因此这是不合适的。输出需要一个用逗号分隔的csv。请告知如何将其与其父级并置?或者有更简单的方法来解析具有不同子节数的xml。

预期产量:

10220,U,988009,2019-12-12
10221,U,988010,2019-12-13,988011,2019-12-14
michael.hor257k:

您可以使用以下样式表轻松获得显示的输出:

XSLT 1.0

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

<xsl:template match="/Integration">
    <xsl:for-each select="ORG/Thing">
        <xsl:value-of select="object"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="type"/>
        <xsl:text>,</xsl:text>
        <xsl:for-each select="dyn/items">
            <xsl:value-of select="val"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="datebegin"/>
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

请注意,每个输出都有一组列items这不是理想的CSV结构。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章