title: XML to CSV using XSLT
body: <p>I am looking for an intelligent efficient XSLT which does convert an XML document to CSV data. It should take care of all possible elements in the child nodes.
  For example, the XML looks like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;sObjects&gt;
     &lt;sObject&gt;
       &lt;Name&gt;Raagu&lt;/Name&gt;
       &lt;BillingStreet&gt;Hoskote&lt;/BillingStreet&gt;
     &lt;/sObject&gt;
     &lt;sObject&gt;
        &lt;Name&gt;Rajath&lt;/Name&gt;
        &lt;BillingStreet&gt;BTM&lt;/BillingStreet&gt;
        &lt;age&gt;25&lt;/age&gt;
     &lt;/sObject&gt;
     &lt;sObject&gt;
        &lt;Name&gt;Sarath&lt;/Name&gt;
        &lt;BillingStreet&gt;Murgesh&lt;/BillingStreet&gt;
        &lt;location&gt;Bangalore&lt;/location&gt;
     &lt;/sObject&gt;
  &lt;/sObjects&gt;
  </code></pre>
  
  <p>And my out put CSV should look like this</p>
  
  <pre><code>Name,BillingStreet,age,location
  Raagu,Hoskote,,
  Rajath,BTM,25,
  Sarath,Murgesh,,Bangalore
  </code></pre>
  
  <p>All the rows should have fields for all the keys in the CSV even though if the XML does have a value for it.</p>
  
  <p>Following is the XSLT code I came up with by looking at different examples over here.</p>
  
  <p>This is the XSLT I have come up with </p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="text"/&gt;
      &lt;xsl:variable name="delimiter" select="','"/&gt;
  
      &lt;xsl:key name="field" match="sObject/*" use="name()"/&gt;
  
      &lt;xsl:template match="/"&gt;
  
          &lt;xsl:for-each select="/*/*/*[generate-id()=generate-id(key('field', name())[1])]"&gt;
              &lt;xsl:value-of select="name()"/&gt;
  
              &lt;xsl:if test="position() != last()"&gt;
                  &lt;xsl:value-of select="$delimiter"/&gt;
              &lt;/xsl:if&gt;
           &lt;/xsl:for-each&gt;
  
          &lt;xsl:text&gt;&amp;#xa;&lt;/xsl:text&gt;
  
          &lt;xsl:for-each select="/*/sObject"&gt;
  
              &lt;xsl:variable name="property" select="." /&gt;
              &lt;xsl:for-each select="$property/*"&gt;
  
                  &lt;xsl:variable name="value" select="." /&gt;
                  &lt;xsl:value-of select="$value"/&gt;
                  &lt;xsl:if test="position() != last()"&gt;
                      &lt;xsl:value-of select="$delimiter"/&gt;
                  &lt;/xsl:if&gt;
                  &lt;xsl:if test="position() = last()"&gt;
                      &lt;xsl:text&gt;&amp;#xa;&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
  
               &lt;/xsl:for-each&gt;
  
          &lt;/xsl:for-each&gt;
  
  
       &lt;/xsl:template&gt;
   &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>and it print this out put</p>
  
  <pre><code>Name,BillingStreet,age,location
  Raagu,Hoskote
  Rajath,BTM,25
  Sarath,Murgesh,Bangalore
  </code></pre>
  
  <p>But I wanted all the rows should contain values for those many times for all the keys on the first row.</p>
  
  <p>Could you please help me achieve this using XSLT code ?</p>