I'm trying to convert xml to csv for the below xml. The structure of the input xml is given below. My xslt is not printing the second "Header" in a new line.
In my XSLT I used for-each and using an if condition to check if it is last node or not, but it is considering last node for an individual chunk and not the last node of the whole xml file. What needs to be tweaked in the existing xslt?
<?xml version="1.0" encoding="UTF-8"?>
<document>
<businessobjects>
<entry>
<HeaderLine>
<HeaderRow>H</HeaderRow>
<HeaderBusinessUnit>BU1</HeaderBusinessUnit>
</HeaderLine>
<LineDetails>
<Line>L</Line>
<Amount>100.00</Amount>
</LineDetails>
</entry>
<entry>
<HeaderLine>
<HeaderRow>H</HeaderRow>
<HeaderBusinessUnit>BU2</HeaderBusinessUnit>
</HeaderLine>
<LineDetails>
<Line>L</Line>
<Amount>200.00</Amount>
</LineDetails>
</entry>
</businessobjects>
</document>
Expected Output
===============
H|BU1
L|100.00
H|BU2
L|200.00
XSLT
====
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="newline" select="' '"/>
<xsl:template match="/">
<xsl:apply-templates select="//entry"/>
</xsl:template>
<xsl:template match="entry">
<xsl:for-each select="HeaderLine">
<xsl:value-of select="concat(HeaderRow,'|',HeaderBusinessUnit,$newline)"/>
</xsl:for-each>
<xsl:for-each select="LineDetails">
<xsl:if test="position()!=last()">
<xsl:value-of select="concat(Line,'|',Amount,$newline)"/>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:value-of select="concat(Line,'|',Amount)"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output with above xslt
======================
H|BU1
L|100.00H|BU2
L|200.00
entry
. – michael.hor257k