title: Using xslt to parse and then format names
body: <p>I'm attempting to write an XSLT stylesheet that will handle author's names and create APA versions of the citation. The format for APA citation regarding author's name(s): Names are listed last name, then initials, if name(s) is the first element of the citation. Separate names with a comma, and an ampersand (&amp;) before the last author. I followed Dimitre Novatchev's solution in this post:<a href="https://stackoverflow.com/questions/13665638/using-xslt-to-select-after-each-instance-in-a-string-substring">Using XSLT to select after EACH instance in a string/substring</a> but I'm not getting the results I wanted.</p>
  <p><b>Input:</b></p>
  <pre><code>&lt;names&gt;
      &lt;author&gt;Lio-Po, Gilda D.&lt;/author&gt;
      &lt;author&gt;Primavera, Jurgenne H.&lt;/author&gt;
      &lt;author&gt;Cuvin-Aralar, Ma. Lourdes A.&lt;/author&gt;
      &lt;author&gt;Cruz, E.R.&lt;/author&gt;
      &lt;author&gt;Catacutan, M.R.&lt;/author&gt;
      &lt;author&gt;Agbayani, R.F.&lt;/author&gt;
  &lt;/names&gt;
  </code></pre>
  <p>
  The desired output would be: <b>Lio-Po, G. D., Primavera, J. H., Cuvin-Aralar, M. L. A., Cruz, E. R., Catacutan, M. R., & Agbayani, R. F.</b><br/>
  </p>
  <p><b>For records with just 2 authors:</b></p>
  <pre><code>&lt;names&gt;
   &lt;author&gt;Lio-Po, Gilda D.&lt;/author&gt;
   &lt;author&gt;Primavera, Jurgenne H.&lt;/author&gt;
  &lt;/names&gt;
  </code></pre>
  <p>
  The desired output would be: <b>Lio-Po, G. D., & Primavera, J. H.</b></p>
  <p>Thanks in advance. Below is my code with some code taken from Dimitre's.</p>
  <pre><code>&lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  
  &lt;xsl:template match=&quot;/&quot;&gt;
      &lt;xsl:for-each select=&quot;/names/author&quot;&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test=&quot;count(following-sibling::text()) = 1&quot;&gt;
                  &lt;xsl:text&gt;&amp;amp; &lt;/xsl:text&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:when test=&quot;count(following-sibling::text()) != 0&quot;&gt;
                  &lt;xsl:apply-templates/&gt;
                  &lt;xsl:text&gt;, &lt;/xsl:text&gt;
              &lt;/xsl:when&gt;
          &lt;/xsl:choose&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;text()&quot;&gt;
      &lt;xsl:value-of select=&quot;substring-before(., ',')&quot;/&gt;
  
      &lt;xsl:call-template name=&quot;replaceTokenDelims&quot;&gt;
          &lt;xsl:with-param name=&quot;pStr&quot; select=&quot;concat(normalize-space(substring-after(., ',')), ' ')&quot;/&gt;
          &lt;xsl:with-param name=&quot;pToken&quot; select=&quot;' '&quot;/&gt;
          &lt;xsl:with-param name=&quot;pReplacement&quot; select=&quot;', '&quot;/&gt;
      &lt;/xsl:call-template&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name=&quot;replaceTokenDelims&quot;&gt;
      &lt;xsl:param name=&quot;pStr&quot;/&gt;
      &lt;xsl:param name=&quot;pToken&quot;/&gt;
      &lt;xsl:param name=&quot;pReplacement&quot;/&gt;
  
      &lt;xsl:if test=&quot;$pStr&quot;&gt;
          &lt;xsl:value-of select=&quot;$pReplacement&quot;/&gt;
          &lt;xsl:value-of select=&quot;substring(substring-before($pStr, $pToken),1,1)&quot;/&gt;
  
          &lt;xsl:call-template name=&quot;replaceTokenDelims&quot;&gt;
              &lt;xsl:with-param name=&quot;pStr&quot; select=&quot;substring-after($pStr, $pToken)&quot;/&gt;
              &lt;xsl:with-param name=&quot;pToken&quot; select=&quot;$pToken&quot;/&gt;
              &lt;xsl:with-param name=&quot;pReplacement&quot; select=&quot;$pReplacement&quot;/&gt;
          &lt;/xsl:call-template&gt;
      &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  <p> Running the above code gives me the output: <b>Lio-Po, G, D, Primavera, J, H, Cuvin-Aralar, M, L, A, Cruz, E, Catacutan, M, & Agbayani, R</b></p>