title: XSLT to print values of a node based on its position repetitively
body: <p>I have to transform this sample xml in xslt 1.0 or xslt 2.0
  <strong>Source XML</strong></p>
  
  <pre><code>&lt;root&gt;
      &lt;group&gt;
          &lt;subgroup&gt;
              &lt;name&gt;AAA1&lt;/name&gt;
          &lt;/subgroup&gt;
          &lt;subgroup&gt;
              &lt;name&gt;AAA2&lt;/name&gt;
          &lt;/subgroup&gt;
          &lt;subgroup&gt;
              &lt;name&gt;AAA3&lt;/name&gt;
         &lt;/subgroup&gt;
      &lt;/group&gt;
   &lt;group&gt;
          &lt;subgroup&gt;
              &lt;name&gt;BB1&lt;/name&gt;
          &lt;/subgroup&gt;
          &lt;subgroup&gt;
              &lt;name&gt;BB2&lt;/name&gt;
          &lt;/subgroup&gt;
          &lt;subgroup&gt;
              &lt;name&gt;BB3&lt;/name&gt;
         &lt;/subgroup&gt;
      &lt;/group&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p><strong>Required XML OUTPUT</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
    &lt;group&gt;AAA1|BB1|AAA2|BB2|AAA3|BB3&lt;/group&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I tried the following xslt: </p>
  
  <p><strong>XSLT</strong></p>
  
  <pre><code>    &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="xml" indent="yes" /&gt;
      &lt;xsl:template match="root"&gt;
      &lt;root&gt;
        &lt;group&gt;
         &lt;xsl:for-each select="group"&gt;
            &lt;xsl:for-each select="subgroup"&gt;
              &lt;xsl:choose&gt;
              &lt;xsl:when test="position() != last()"&gt;
              &lt;xsl:value-of select="name" /&gt;&lt;xsl:text&gt;|&lt;/xsl:text&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                 &lt;xsl:value-of select="name" /&gt;
              &lt;/xsl:otherwise&gt;
               &lt;/xsl:choose&gt; 
            &lt;/xsl:for-each&gt;
         &lt;/xsl:for-each&gt;
         &lt;/group&gt;
      &lt;/root&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Current-output</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
  &lt;group&gt;AAA1|AAA2|AAA3BB1|BB2|BB3&lt;/group&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>How can I do this? Should I create a template a call it recursively?</p>