将XML转换为CSV(使用XSLT)(Converting XML to CSV (using XS

2019-10-28 09:01发布

我有下面的XML代码,需要将其转换为.csv文件。 我不需要整个XML文件,但我不得不选择一些XML标记。

<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>    

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

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吗? 我搜索了类似的问题的解决方案,但没有成功。 我希望选择一些标签,不是所有变量的数值。

Answer 1:

下面将XSL提供所需的输出。

<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;


Answer 2:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="iso-8859-1"/>

  <xsl:strip-space elements="*" />

  <xsl:template match="/">
    <xsl:text>Reference;Make;Invoice_Id;Invoice_Description</xsl:text>
    <xsl:text>&#xa;</xsl:text>
    <xsl:apply-templates select="contract_Set"/>     
</xsl:template>

  <xsl:template match="contract_period">
      <xsl:value-of select="reference"/>;<xsl:value-of select="vehicle_Set/vehicle/make"/>;<xsl:value-of select="invoice_Set/invoice/id"/>;<xsl:value-of select="invoice_Set/invoice/description"/>;
  </xsl:template>
</xsl:transform>


文章来源: Converting XML to CSV (using XSLT)
标签: xml csv xslt