rockstar_77 rockstar_77 - 5 months ago 14
Java Question

An empty line in csv output file using xslt

Hi i am have an XML file and trying to convert it using xslt. But issue is i get a white line before the output.How do i eliminate it.
Below is my XML

<?xml version="1.0" encoding="UTF-8"?>
<UserInfo xmlns="http://XXXXXX">
<User>
<UserName>MNO</UserName>
<Userid>1234</Userid>
<address>xyz</address>
<city>ABC</city>
<state>XX</state>
<zip>000000</zip>
</User>
<User>
<UserName>DEF</UserName>
<Userid>4567</Userid>
<address>IJK</address>
<city>GHI</city>
<state>XX</state>
<zip>000000</zip>
</User>

</UserInfo>


below is my xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
UserName,Userid,address,city,state,zip
<xsl:for-each select="//User">
<xsl:value-of select="concat(UserName,',',Userid,',',address,',',city,',',state,',',zip,'&#xA;')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


When ever i run the above i get a white line first and then the output.

vendorName,vendorId,vendorTaxId,addressLine1,addressLine2,city,state,zip
MNO,1234,xyz,ABC,XX,000000
DEF,4567,IJK,GHI,XX,000000


I get the white space above the nodes(UserName,UserId etc)

Answer

Use xsl:text to output literal text, e.g.:

<xsl:template match="/">
    <xsl:text>UserName,Userid,address,city,state,zip&#xA;</xsl:text>
    <xsl:for-each select="//User">
        <xsl:value-of select="concat(UserName,',',Userid,',',address,',',city,',',state,',',zip,'&#xA;')"/>
    </xsl:for-each>
</xsl:template>

The way you have it now, the entire text node is written to the output - including the line break between <xsl:template match="/"> and vendorName.