将 XML 转换为 CSV(使用 XSLT) [英] Converting XML to CSV (using XSLT)

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

问题描述

我有以下 XML 代码,需要将其转换为 .csv 文件.我不需要整个 xml 文件,但我必须选择一些 xml 标签.

I have the following XML code, and need to convert this to a .csv file. I don't need the whole xml file, but i have to select some xml tags.

<contract_Set>
<contract_period>
    <reference>111111</reference>
    <startperiod>2017-09-01</startperiod>
    <endperiod>2017-09-06</endperiod>
    <vehicle_Set>
      <vehicle>
        <vehicle_id>4444</vehicle_id>
        <make>Mercedes-Benz</make>
      </vehicle>
    </vehicle_Set>
    <invoice_Set>
      <invoice>
        <id>12345</id>
        <description>Some text</description>
      </invoice>
    </invoice_Set>
    <invoice_Set>
      <invoice>
        <id>12222</id>
        <description>More text</description>
      </invoice>
    </invoice_Set>
</contract_period>
<contract_period>
    <reference>222222</reference>
    <startperiod>2017-09-01</startperiod>
    <endperiod>2017-09-30</endperiod>
    <vehicle_Set>
      <vehicle>
        <vehicle_id>55555</vehicle_id>
        <make>Audi</make>
      </vehicle>
    </vehicle_Set>
    <invoice_Set>
      <invoice>
        <id>45678</id>
        <description>Audi text</description>
      </invoice>
    </invoice_Set>
</contract_period></contract_Set>    

我需要我的输出是这样的:

I need my output to be like:

Reference;Make;Invoice_Id;Invoice_Description
111111;Mercedes-Benz;12345;Some text
111111;Mercedes-Benz;12222;More text
222222;Audi;45678;Audi text

我怎样才能用 XSLT 1.0 做到这一点?我已经在类似问题中寻找解决方案,但没有成功.我希望选择某些标签的值,而不是所有标签.

How can i do this with XSLT 1.0? I have searched for solutions in similar questions, but no success. I'm looking to select values of some tags, not all tags.

推荐答案

以下 XSL 将提供所需的输出.

The following XSL will provide the desired output.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:variable name="separator" select="'&#59;'" />
    <xsl:variable name="newline" select="'&#10;'" />

    <xsl:template match="/">
        <xsl:text>Reference;Make;Invoice_Id;Invoice_Description</xsl:text>
        <xsl:value-of select="$newline" />
        <xsl:for-each select="//invoice_Set">
            <xsl:value-of select="../reference" />
            <xsl:value-of select="$separator" />
            <xsl:value-of select="../vehicle_Set/vehicle/make" />
            <xsl:value-of select="$separator" />
            <xsl:value-of select="invoice/id" />
            <xsl:value-of select="$separator" />
            <xsl:value-of select="invoice/description" />
            <xsl:value-of select="$separator" />
            <xsl:value-of select="$newline" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出

Reference;Make;Invoice_Id;Invoice_Description
111111;Mercedes-Benz;12345;Some text;
111111;Mercedes-Benz;12222;More text;
222222;Audi;45678;Audi text;

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

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