title: Extract CSV from XLM and Write back to XML format Using Stylesheet
body: <p>I want to extract the data on an xml file into a csv file then manipulate the data and write the manipulated data back into the same xml format using a stylesheet.</p>
  <p><strong>Input XML</strong></p>
  <pre><code>&lt;population&gt;
    &lt;!-- ====================================================================== --&gt; 
   &lt;person id=&quot;person_203&quot;&gt;
      &lt;attributes&gt;
        &lt;attribute name=&quot;student&quot; class=&quot;java.lang.String&quot;&gt;FALSE&lt;/attribute&gt;
        &lt;attribute name=&quot;subpopulation&quot; class=&quot;java.lang.String&quot;&gt;person&lt;/attribute&gt;
        &lt;attribute name=&quot;tbc&quot; class=&quot;java.lang.String&quot;&gt;0&lt;/attribute&gt;
        &lt;attribute name=&quot;wards&quot; class=&quot;java.lang.String&quot;&gt;19100100&lt;/attribute&gt;
      &lt;/attributes&gt;
      &lt;plan selected=&quot;yes&quot;&gt;
        &lt;activity type=&quot;home&quot; x=&quot;-491818.676117819&quot; y=&quot;-3702897.1885688&quot; end_time=&quot;06:24:13&quot; /&gt;
        &lt;leg mode=&quot;pt&quot; /&gt;
        &lt;activity type=&quot;leisure&quot; x=&quot;-491101.476702402&quot; y=&quot;-3703311.7449867&quot; end_time=&quot;11:15:07&quot; /&gt;
        &lt;leg mode=&quot;pt&quot; /&gt;
        &lt;activity type=&quot;shopping&quot; x=&quot;-494740.864728234&quot; y=&quot;-3702228.87495458&quot; end_time=&quot;12:59:01&quot; /&gt;
        &lt;leg mode=&quot;pt&quot; /&gt;
        &lt;activity type=&quot;home&quot; x=&quot;-491818.676117819&quot; y=&quot;-3702897.1885688&quot; /&gt;
      &lt;/plan&gt;
    &lt;/person&gt;
      &lt;!-- ====================================================================== --&gt;
    &lt;person id=&quot;person_174&quot;&gt;
      &lt;attributes&gt;
        &lt;attribute name=&quot;student&quot; class=&quot;java.lang.String&quot;&gt;FALSE&lt;/attribute&gt;
        &lt;attribute name=&quot;subpopulation&quot; class=&quot;java.lang.String&quot;&gt;person&lt;/attribute&gt;
        &lt;attribute name=&quot;tbc&quot; class=&quot;java.lang.String&quot;&gt;0&lt;/attribute&gt;
        &lt;attribute name=&quot;wards&quot; class=&quot;java.lang.String&quot;&gt;19100061&lt;/attribute&gt;
      &lt;/attributes&gt;
      &lt;plan selected=&quot;yes&quot;&gt;
        &lt;activity type=&quot;home&quot; x=&quot;-491592.958893144&quot; y=&quot;-3703196.68309401&quot; /&gt;
      &lt;/plan&gt;
    &lt;/person&gt;
    &lt;!-- ====================================================================== --&gt;
   &lt;/population&gt;
  </code></pre>
  <p><strong>Expected csv output</strong></p>
  <pre><code>id,Student,subpopulation,tbc,wards,Selected,type,x,y,end_time,.....
  person_203,FALSE,person,0,19100100,yes,home,-491818,6761,-3702897,189,06:24:13,....
  person_174,FALSE,person,0,19100061,yes,home,-491592.9589,-3703196.683......
  </code></pre>
  <p>The plan attribute is different for person_203 and person_174 elements. How can I achieve this?</p>
  <p><strong>My Stylesheet is as below:-</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  &lt;xsl:transform version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output method=&quot;text&quot; encoding=&quot;UTF-8&quot;/&gt;
      &lt;xsl:template match=&quot;/&quot;&gt;
          &lt;xsl:text&gt;person,attribute,x, y, &amp;#xA;&lt;/xsl:text&gt;
              &lt;xsl:for-each select=&quot;.//person&quot;&gt;
                      &lt;xsl:value-of select=&quot;concat(@id,'&amp;#9;')&quot;/&gt;
                  &lt;xsl:for-each select=&quot;.//attributes&quot;&gt;
                      &lt;xsl:for-each select=&quot;.//attribute&quot;&gt;
                          &lt;xsl:value-of select=&quot;concat(',',text(),'&amp;#9;')&quot;/&gt; 
                      &lt;/xsl:for-each&gt;
                  &lt;/xsl:for-each&gt;
                  &lt;xsl:for-each select=&quot;.//plan&quot;&gt;
                          &lt;xsl:for-each select=&quot;.//activity&quot;&gt;
                              &lt;xsl:value-of select=&quot;concat(',',',',@x,',',@y,'&amp;#9;')&quot;/&gt;
                          &lt;/xsl:for-each&gt;
                  &lt;/xsl:for-each&gt;
                  
              &lt;/xsl:for-each&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:transform&gt;
  </code></pre>
  <p><strong>The Current output is this:</strong></p>
  <pre><code>person,attribute,x, y, 
  &quot;person_203 &quot;
  &quot; ,FALSE    ,person ,0  ,19100100   ,,-491818.676117819,-3702897.1885688    ,,-491101.476702402,-3703311.7449867    ,,-494740.864728234,-3702228.87495458   ,,-491818.676117819,-3702897.1885688    person_174  &quot;
  &quot; ,FALSE    ,person ,0  ,19100061   ,,-491592.958893144,-3703196.68309401   &quot;
  </code></pre>
  <p>I have looked at the two solutions below but the formats of xml are slightly different.</p>
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/365312/xml-to-csv-using-xslt">XML to CSV Using XSLT</a></p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/29295377/xslt-2-0-to-convert-csv-to-xml-format">XSLT 2.0 to convert CSV to XML format</a></p>
  </li>
  </ul>