Reputation: 29
I have gone thru this solution,but it covers in a single level of nesting, my requirement is thru multilevel of nesting and putting the values in the same row
Convert an XML file to CSV file using java
I have this format of input of xml below .
<?xml version="1.0"?>
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>ABC/123</MsgId>
<NbOfTxs>1000</NbOfTxs>
<InitgPty>
<Nm>ABC</Nm>
</InitgPty>
<PmtInf>
<DbtrAcct>
<Id>
<Othr>
<Id>00000007010</Id>
</Othr>
</Id>
</DbtrAcct>
</PmtInf>
</GrpHdr>
</CstmrCdtTrfInitn>
My style.xsl below I tried
<?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="/">
MsgId,NbOfTxs,Nm,ID
<xsl:for-each select="//CstmrCdtTrfInitn">
<xsl:value-of select="concat(MsgId,NbOfTxs,',',Nm,',',ID,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I am getting the nm and ID in another row,
My code below
class Xml2Csv {
public static void main(String args[]) throws Exception {
File stylesheet = new File("src/main/resources/style.xsl");
File xmlSource = new File("src/main/resources/data.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlSource);
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(stylesource);
Source source = new DOMSource(document);
Result outputTarget = new StreamResult(new File("/tmp/x.csv"));
transformer.transform(source, outputTarget);
}
}
I would need output like below in CSV
MsgId,NbOfTxs,Nm(Inside the InitgPty tag ),ID((Inside the other tag )) as the header
ABC/123,1000,ABC,00000007010 as values
Upvotes: 0
Views: 522
Reputation: 70638
If you want to access nested nodes in your XML, you need to specify the full path to each node in your expression
<xsl:value-of select="concat(GrpHdr/MsgId,GrpHdr/NbOfTxs,',',GrpHdr/InitgPty/Nm,',',GrpHdr/PmtInf/DbtrAcct/Id/Othr/Id,'
')"/>
You could potentially simplify the statement by selecting GrpHdr
elements instead of just CstmrCdtTrfInitn
.
Try this XSLT:
<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="/">
<xsl:text>MsgId,NbOfTxs,Nm,ID</xsl:text>
<xsl:for-each select="//CstmrCdtTrfInitn/GrpHdr">
<xsl:value-of select="concat(MsgId,NbOfTxs,',',InitgPty/Nm,',',PmtInf/DbtrAcct/Id/Othr/Id,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1