Advertisements

Tuesday, August 8, 2017

XML to a File(CSV) in Eclipse

Download the plugin: Orangevolt Eclipse XSLT

Can be found here with explanation of how to install it:

http://www.ibm.com/developerworks/library/os-eclipse-orangevolt/

I did the manual install since I got an error trying to update it from the reposutory, what I did was:
  • Download the zip
  • Uncompress and look for feature and plugin folders.
  • Put whatever is in feature and go to the eclipse installation and paste it in the same format
  • Do the same with plugin folder, put the jar files into the eclipse/plugin
Restart eclipse.
The examples below where extracted for stackoverflow: 

http://stackoverflow.com/questions/23263808/using-xsl-transformation-for-xml-to-csv-conversion

This is another option to do the break line:


 <xsl:if test="not(position()=last())">
   <xsl:text>&#xA;</xsl:text>
 </xsl:if>
and
<?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="/">
      <xsl:value-of select="concat('RowValue,Value', '&#xA;')"/>
      <xsl:for-each select="//RowOfValues[1]/RowValue">
         <xsl:variable name="pos" select="position()"/>
         <xsl:value-of select="normalize-space(.)"/>
         <xsl:call-template name="ScrapeColumns">
            <xsl:with-param name="pos" select="$pos"/>
         </xsl:call-template>
         <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
   </xsl:template>

   <xsl:template name="ScrapeColumns">
      <xsl:param name="pos"></xsl:param>
      <xsl:for-each select="//RowOfValues[position() > 1]//RowValue[position()=$pos]">
         <xsl:value-of select="concat(', ', normalize-space(.))"/>
      </xsl:for-each>   
   </xsl:template>
</xsl:stylesheet>
and

<?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="/">
      <xsl:value-of select="concat('RowValue,Value', '&#xA;')"/>
      <xsl:for-each select="//RowOfValues[1]/RowValue">
         <xsl:variable name="pos" select="position()"/>
         <xsl:value-of select="concat(normalize-space(
                       concat(., ',', //RowOfValues[2]/RowValue[position()=$pos])),
                    '&#xA;')"/>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

No comments:

Post a Comment