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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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 www.javaranch.com and modified as bellow and got it working:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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]
jQuery in Action, 2nd edition
subject: Q: XML to CSV