neděle 7. července 2013

Convert XML to CSV example

Input CSV

 <?xml version="1.0" encoding="utf-8"?>  
 <detail id="1243">  
  <nazev>Praktický lékař pro dospělé</nazev>  
  <jmeno>MUDr. Nomen Omen</jmeno>  
  <adresa>Street 1/234, 18000 City 8</adresa>  
  <telefon>89879076</telefon>  
  <fax/>  
  <ico>8908078</ico>  
  <poradove-cislo>000</poradove-cislo>  
  <poradove-cislo-det-prac>000</poradove-cislo-det-prac>  
  <nazev-zrizovatele>Fyzická osoba</nazev-zrizovatele>  
  <druh-zz>320 Samost.ordinace prakt.lék.pro dospělé</druh-zz>  
  <email>nomen-omen@mailer.ru</email>  
  <www/>  
 </detail>  

XSLT template file - takes one parametr output-header - when true, header is output first, output is Tab separated, no escaping nor quoting is done

 <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:param name="output-header" value="false"/>  
  <xsl:template match="/">  
   <xsl:if test="$output-header='true'">  
    <xsl:value-of select="concat('id','&#x09;')"/>  
    <xsl:value-of select="concat('type','&#x09;')"/>  
    <xsl:value-of select="concat('nazev','&#x09;')"/>  
    <xsl:value-of select="concat('jmeno','&#x09;')"/>  
    <xsl:value-of select="concat('adresa','&#x09;')"/>  
    <xsl:value-of select="concat('telefon','&#x09;')"/>  
    <xsl:value-of select="concat('fax','&#x09;')"/>  
    <xsl:value-of select="concat('ico','&#x09;')"/>  
    <xsl:value-of select="concat('poradove-cislo','&#x09;')"/>  
    <xsl:value-of select="concat('poradove-cislo-det-prac','&#x09;')"/>  
    <xsl:value-of select="concat('nazev-zrizovatele','&#x09;')"/>  
    <xsl:value-of select="concat('druh-zz','&#x09;')"/>  
    <xsl:value-of select="concat('email','&#x09;')"/>  
    <xsl:value-of select="concat('www','')"/>  
    <xsl:text>  
 </xsl:text>  
   </xsl:if>  
   <xsl:for-each select="//detail">  
    <xsl:value-of select="concat(@id,'&#x09;')"/>  
    <xsl:value-of select="concat('detail','&#x09;')"/>  
    <xsl:value-of select="concat(nazev,'&#x09;')"/>  
    <xsl:value-of select="concat(jmeno,'&#x09;')"/>  
    <xsl:value-of select="concat(adresa,'&#x09;')"/>  
    <xsl:value-of select="concat(telefon,'&#x09;')"/>  
    <xsl:value-of select="concat(fax,'&#x09;')"/>  
    <xsl:value-of select="concat(ico,'&#x09;')"/>  
    <xsl:value-of select="concat(poradove-cislo,'&#x09;')"/>  
    <xsl:value-of select="concat(poradove-cislo-det-prac,'&#x09;')"/>  
    <xsl:value-of select="concat(nazev-zrizovatele,'&#x09;')"/>  
    <xsl:value-of select="concat(druh-zz,'&#x09;')"/>  
    <xsl:value-of select="concat(email,'&#x09;')"/>  
    <xsl:value-of select="concat(www,'')"/>  
    <xsl:text>  
 </xsl:text>  
   </xsl:for-each></xsl:template>  
 </xsl:stylesheet>  

Customization: use your own element names, template matching rule and whatever to process more complex XMLs. Keep the structure (spaces, line ends) of the  tailing <xsl:text> to get proper line ends at the right places.