This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Q: XML to CSV Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Q: XML to CSV" Watch "Q: XML to CSV" New topic


Chuck Zheng

Joined: Apr 03, 2004
Posts: 5
I am a newbie to XSLT. Just try to convert following XML files to csv
<?xml version="1.0" encoding="UTF-8"?>
convert to
I also need to convert another similar XML file:
<?xml version="1.0" encoding="UTF-8"?>
convert to
I found some handy work on and modified as bellow and got it working:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="" version="1.0">
<xsl utput method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="DEP/Department|DIV/Division">
<xsl:apply-templates select="*"/>
<xsl:template match="*">
<!-- No comma on the last element -->
<xsl:when test="position()=last()"><xsl:value-of select="."/><xsl:text>
<!-- Comma required. Not the last element -->
<xsl therwise><xsl:value-of select="."/>,</xsl therwise>
My question is how not to write "DEP/Department|DIV/Division" in the xsl:for-each?
this is because I have more similar format files to process. any functions I can use?
Or there are even better solution out there? I wish some kind sole can shed some light
for me.
Thanks very much in advance
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Would it be possible to convert those DIV, DEP, etc. into parameters which you specify when running the XSLT engine? I'm not sure if it's possible to use input parameters for this particular purpose...

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: Q: XML to CSV
jQuery in Action, 3rd edition