A
A
Alexey2020-08-19 20:46:37
Java
Alexey, 2020-08-19 20:46:37

How to convert complex Xml to csv?

Hello! I am compiling a program in java, I really need help with xslt conversion. It is necessary to make a csv file from xml.
I got the following xslt filter:

<?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 with the following structure:
<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>

At the output, I get lines separated by commas, and a few more lines (the same items) with values ​​below. and I can't figure out how to glue the values...
I would do it through value-of select="concat" but I may have several dyn( 1, 2, 3 ... ), so this is not suitable.
The output needs csv separated by commas.
Please advise how to glue item with its parent? Or there are simpler ways to parse xml with different number of subsections.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question