title: Multiple rows using XSLT
body: <p>I appreciate if anyone could help me on this. I have an XML that looks like this:</p>
  
  <pre><code>&lt;root&gt;
  &lt;worker&gt;
       &lt;Primary_Key&gt;12345&lt;/Primary_Key&gt;
       &lt;First_Name&gt;ABC&lt;/First_Name&gt;
       &lt;Last_Name&gt;DEF&lt;/Last_Name&gt;         
      &lt;Multiple_Data&gt;
          &lt;Code&gt;MO&lt;/Code&gt;
          &lt;Amount&gt;35&lt;/Amount&gt;
      &lt;/Multiple_Data&gt;
      &lt;Multiple_Data&gt;
          &lt;Code&gt;PA&lt;/Code&gt;
          &lt;Amount&gt;45&lt;/Amount&gt;
      &lt;/Multiple_Data&gt;
  &lt;/worker&gt;
  &lt;worker&gt;
      &lt;Primary_Key&gt;67890&lt;/Primary_Key&gt;
      &lt;First_Name&gt;GHI&lt;/First_Name&gt;
      &lt;Last_Name&gt;JKL&lt;/Last_Name&gt;
      &lt;Multiple_Data&gt;
          &lt;Code&gt;PA&lt;/Code&gt;
          &lt;Amount&gt;25&lt;/Amount&gt;
      &lt;/Multiple_Data&gt;
  &lt;/worker&gt;
  &lt;worker&gt;
      &lt;Primary_Key&gt;11121&lt;/Primary_Key&gt;
      &lt;First_Name&gt;MNO&lt;/First_Name&gt;
      &lt;Last_Name&gt;PQR&lt;/Last_Name&gt;    
  &lt;/worker&lt;/root&gt;
  </code></pre>
  
  <p>And I need to have the output that will exactly looks like this:  </p>
  
  <blockquote>
    <p>12345,ABC,DEF,MO,35<br>
    12345,ABC,DEF,PA,45<br>
    67890,GHI,JKL,PA,25<br>
    11121,MNO,PQR,,</p>
  </blockquote>
  
  <p>12345 has multiple data Codes and Amounts. Values are displayed on separate rows. 11121 doesn't have code and amount so the delimiter ,, is the only one displayed. </p>
  
  <p>Here's the XSLT but seems not working:    </p>
  
  <p></p>
  
  <pre><code>&lt;output method="text"/&gt;
  
  &lt;variable name="delimiter"&gt;&lt;text&gt;,&lt;/text&gt;&lt;/variable&gt;    
  &lt;variable name="linefeed" select="'&amp;#xd;&amp;#xa;'"/&gt;
  &lt;template match="child::Primary_Key"&gt;     
      &lt;choose&gt;
          &lt;when test="preceding-sibling::Multiple_Data//child::text() = ''"&gt;
              &lt;for-each select="following-sibling::Multiple_Data"&gt;
  
                  &lt;value-of select="concat(preceding-sibling::Primary_Key/child::text(), $delimiter, preceding-sibling::First_Name/child::text(), $delimiter, preceding-sibling::Last_Name/child::text(), $delimiter, child::Code/child::text(), $delimiter, child::Amount/child::text())"/&gt;
  
                  &lt;if test="(self::Primary_Key[position() = last()])"&gt;
                      &lt;value-of select="$linefeed"/&gt; 
                  &lt;/if&gt;
  
              &lt;/for-each&gt;
          &lt;/when&gt;
          &lt;otherwise&gt;
              &lt;for-each select="child::Primary_Key"&gt;                    
                  &lt;value-of select="concat(preceding-sibling::Primary_Key/child::text(), $delimiter, preceding-sibling::First_Name/child::text(), $delimiter, preceding-sibling::Last_Name/child::text(), $delimiter, $delimiter )"/&gt;                
                  &lt;if test="(self::Primary_Key[position() = last()])"&gt;
                      &lt;value-of select="$linefeed"/&gt; 
                  &lt;/if&gt;
  
              &lt;/for-each&gt;
  
          &lt;/otherwise&gt;
  
      &lt;/choose&gt;                        
  &lt;/template&gt;
  &lt;template match="child::root/worker"&gt;
      &lt;apply-templates select="child::Primary_Key"/&gt;
  &lt;/template&gt;
  &lt;/transform&gt;
  </code></pre>
  
  <p>When I try to use this below XSLT, it's giving me unnecessary new line at the top and spaces. if(position() > 1) is not working to get rid the first blank line in my output. And of course, worker who doesn't have multiple data is not displayed here.</p>
  
  <pre><code>&lt;transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  
  &lt;output method="text"/&gt;
  
  &lt;variable name="delimiter"&gt;&lt;text&gt;,&lt;/text&gt;&lt;/variable&gt;    
  &lt;variable name="linefeed" select="'&amp;#xd;&amp;#xa;'"/&gt;
  &lt;template match="child::Primary_Key"&gt;     
  
         &lt;for-each select="following-sibling::Multiple_Data"&gt;
  
                  &lt;value-of select="concat(preceding-sibling::Primary_Key/child::text(), $delimiter, preceding-sibling::First_Name/child::text(), $delimiter, preceding-sibling::Last_Name/child::text(), $delimiter, child::Code/child::text(), $delimiter, child::Amount/child::text())"/&gt;                
                  &lt;if test="(self::Primary_Key[position() = last()])"&gt;
                      &lt;value-of select="$linefeed"/&gt; 
                  &lt;/if&gt;
  
              &lt;/for-each&gt;
  
  &lt;/template&gt;
  &lt;template match="child::root/worker"&gt;
      &lt;apply-templates select="child::Primary_Key"/&gt;
  &lt;/template&gt;
  </code></pre>
  
  <p></p>
  
  <p>Are there any other better ways to code this without using this transform in the heading?</p>
  
  <blockquote>
  <pre><code>&lt;transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  </code></pre>
  </blockquote>
  
  <p>I'd like to create the xslt starting with: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
  </code></pre>
  
  <p>but looks like the child, preceding-sibling and following-sibling:: codes are not supported by that. </p>
  
  <p>Thanks a lot for your help.</p>