title: xml to csv through xslt transformation
body: <p>I know there are a lot of questions on this, but I am stuck on the problem of converting xml to csv using xslt. I used an example xslt for testing:</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>This code works well on 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>The example I found on SO, but could anyone give me a hint on how to process an xml, which has more depth in it? Say:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;sObjects&gt;
     &lt;sObject&gt;
       &lt;Person&gt;
         &lt;Name&gt;Raagu&lt;/Name&gt;
         &lt;BillingStreet&gt;Hoskote&lt;/BillingStreet&gt;
       &lt;/Person&gt;
       &lt;Country&gt;Germany&lt;/Country&gt;
     &lt;/sObject&gt;
  &lt;sObjects&gt;
  </code></pre>
  
  <p>I will be very grateful for any hint on this. Thanks in advance.</p>