title: Looping through nodes in XSLT
body: <p>Please help on XSLT, I have this XML sample</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Document&gt;
    &lt;TopLevel&gt;
      &lt;Header&gt;
        &lt;!-- Header Information--&gt;
      &lt;/Header&gt;
      &lt;!--Payments is a one to many--&gt;
      &lt;Payments&gt;
        &lt;PaymentID&gt;PID1&lt;/PaymentID&gt;
        &lt;!--More Info--&gt;
        &lt;!--Transactrion can be one to many for each payment--&gt;
        &lt;Transaction&gt;
          &lt;TranssID&gt;TR1 - PID1&lt;/TranssID&gt;
          &lt;TranssID&gt;TR2 - PID1&lt;/TranssID&gt;
          &lt;!--More Info--&gt;
        &lt;/Transaction&gt;
      &lt;/Payments&gt;
      &lt;Payments&gt;
        &lt;PaymentID&gt;PID2&lt;/PaymentID&gt;
        &lt;!--More Info--&gt;
        &lt;!--Transactrion can be one to many for each payment--&gt;
        &lt;Transaction&gt;
          &lt;TranssID&gt;TR1 - PID2&lt;/TranssID&gt;
          &lt;TranssID&gt;TR2 - PID2&lt;/TranssID&gt;
          &lt;!--More Info--&gt;
        &lt;/Transaction&gt;
      &lt;/Payments&gt;
    &lt;/TopLevel&gt;
  &lt;/Document&gt;
  </code></pre>
  
  <p>I need a looping mechanism in XSLT that will give me the transactions for each payments.</p>
  
  <p>Here is the output when I tried to use the template approach
  OUTPUT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;TopLevel&gt;
    &lt;Payments&gt;
      &lt;PaymentID&gt;PID1&lt;/PaymentID&gt;
      &lt;transIDs&gt;TR1 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID2&lt;/transIDs&gt;
    &lt;/Payments&gt;
    &lt;Payments&gt;
      &lt;PaymentID&gt;PID1&lt;/PaymentID&gt;
      &lt;transIDs&gt;TR1 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID1&lt;/transIDs&gt;
      &lt;transIDs&gt;TR1 - PID2&lt;/transIDs&gt;
      &lt;transIDs&gt;TR2 - PID2&lt;/transIDs&gt;
    &lt;/Payments&gt;
  &lt;/TopLevel&gt;
  </code></pre>
  
  <p>I want it to be separated by transaction and by payments</p>
  
  <p>Here is my XSLT</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="1.0"&gt;
  
    &lt;xsl:output method="xml" indent="yes"/&gt;
  
    &lt;xsl:template match="/"&gt;
      &lt;TopLevel&gt;
        &lt;xsl:apply-templates select="//TopLevel/Payments"/&gt;
      &lt;/TopLevel&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="//TopLevel/Payments"&gt;
      &lt;Payments&gt;
        &lt;PaymentID&gt;
          &lt;xsl:value-of select="//TopLevel/Payments/PaymentID"/&gt;
        &lt;/PaymentID&gt;
        &lt;xsl:apply-templates select="//TopLevel/Payments/Transaction"/&gt;
      &lt;/Payments&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="//TopLevel/Payments/Transaction"&gt;
      &lt;xsl:apply-templates select="//TopLevel/Payments/Transaction/TranssID"/&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="//TopLevel/Payments/Transaction/TranssID"&gt;
      &lt;transIDs&gt;
        &lt;xsl:value-of select="."/&gt;
      &lt;/transIDs&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>