title: Generating Flat file output in specific order using XSLT
body: <p>I am trying to convert XML to a flat file. The XML input is:</p>
  
  <pre><code>&lt;PartsPriceListLine&gt;
      &lt;PartsProductItem&gt;
          &lt;ItemID&gt;1234&lt;/ItemID&gt;
          &lt;PartName&gt;abcd&lt;/PartName&gt;
          &lt;PartItemDescription&gt;test&lt;/PartItemDescription&gt;
      &lt;/PartsProductItem&gt;
      &lt;Price&gt;
          &lt;PriceCode&gt;List&lt;/PriceCode&gt;
          &lt;ChargeAmount&gt;544.04&lt;/ChargeAmount&gt;
      &lt;/Price&gt;
      &lt;Price&gt;
          &lt;PriceCode&gt;TradeIn&lt;/PriceCode&gt;
          &lt;ChargeAmount&gt;489.64&lt;/ChargeAmount&gt;
      &lt;/Price&gt;
      &lt;Price&gt;
          &lt;PriceCode&gt;Sold&lt;/PriceCode&gt;
          &lt;ChargeAmount&gt;408.03&lt;/ChargeAmount&gt;
      &lt;/Price&gt;
  &lt;/PartsPriceListLine&gt;
  </code></pre>
  
  <p>The PriceCode values could come in any order, however, the output file should always have a value of ChargeAmount assigned to a DailyPrice column when PriceCode value is 'Sold', to TradePrice Column when PriceCode value is 'TradeIn' and to ListPrice column when PriceCode value is 'List'.</p>
  
  <p>I have written XSLT as below:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>          &lt;xsl:for-each select="s2:PartsPriceListLine/s2:Price"&gt;
              &lt;xsl:variable name="PriceCode" select="string(s2:PriceCode/text())"/&gt;
     &lt;xsl:variable name="ChargeAmount" select="string(s2:ChargeAmount/text())"/&gt;
     
              &lt;xsl:choose&gt;
     &lt;xsl:when test = "$PriceCode = 'Sold'"&gt;
     &lt;DAILY_PRICE&gt;
     
                &lt;xsl:value-of select="concat($ChargeAmount,',')" /&gt;
              &lt;/DAILY_PRICE&gt;
     &lt;/xsl:when&gt;
     &lt;/xsl:choose&gt;
     &lt;xsl:choose&gt;
     &lt;xsl:when test = "$PriceCode = 'Trade-In'"&gt;
     &lt;TRADE_PRICE&gt;
                &lt;xsl:value-of select="concat($ChargeAmount,',')" /&gt;
              &lt;/TRADE_PRICE&gt;
     &lt;/xsl:when&gt; 
     &lt;/xsl:choose&gt;
     &lt;xsl:choose&gt;
     &lt;xsl:when test = "$PriceCode = 'List'"&gt;
     &lt;LIST_PRICE&gt;
                &lt;xsl:value-of select="concat($ChargeAmount,',')" /&gt;
              &lt;/LIST_PRICE&gt;
     &lt;/xsl:when&gt; 
     &lt;/xsl:choose&gt; 
       &lt;/xsl:for-each&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>With above code, the values are being entered in output in the order they are mentioned in XML input file.</p>
  
  <p>The output for above XSLT is:</p>
  
  <blockquote>
    <p>DAILY_PRICE,TRADE_PRICE,LIST_PRICE</p>
    
    <p>544.04,489.64,408.03</p>
  </blockquote>
  
  <p>whereas expected result is:</p>
  
  <blockquote>
    <p>DAILY_PRICE,TRADE_PRICE,LIST_PRICE</p>
    
    <p>408.03,489.64,544.04</p>
  </blockquote>
  
  <p>Please note the output doesn't require any Header/column names. </p>
  
  <p>Any pointer/help in this regard is appreciated.</p>