0

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

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.

Stuart
  • 11
  • 1
  • 3
  • 2
    "*Can i do this with XSLT 1.0?*" Yes. – michael.hor257k Sep 11 '17 at 09:12
  • You can *literally* search for your question title and find two dozen working solutions. Pick one of them. – Tomalak Sep 11 '17 at 09:32
  • 1
    Possible duplicate of [XML to CSV Using XSLT](https://stackoverflow.com/questions/365312/xml-to-csv-using-xslt) – MartinByers Sep 11 '17 at 09:47
  • this i s aduplicate of https://stackoverflow.com/questions/365312/xml-to-csv-using-xslt – MartinByers Sep 11 '17 at 09:47
  • I have been searching for an answer in existing questions..ofcourse. I did not find any solutions where it is possible to select the values of some xml tags, while ignoring others. – Stuart Sep 11 '17 at 10:49
  • Regardless of what solution you do find, one important thing you need to think about- what happens if your Make/Description fields contain a semicolon? – Flynn1179 Sep 11 '17 at 12:03
  • Hi i have tried for your question but i am getting partial output its useful for you or not i dont know i am giving template ; – User515 Sep 11 '17 at 12:21

2 Answers2

2

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>

Output

Reference;Make;Invoice_Id;Invoice_Description
111111;Mercedes-Benz;12345;Some text;
111111;Mercedes-Benz;12222;More text;
222222;Audi;45678;Audi text;
Aniket V
  • 3,183
  • 2
  • 15
  • 27
0
<?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>
User515
  • 186
  • 12
  • Thank you for your answer. Unfortunately, I'm only getting two records in my output. 111111;Mercedes-Benz;12222;More text is missing – Stuart Sep 12 '17 at 10:16