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 (&) before the last author. I followed Dimitre Novatchev's solution in this post:Using XSLT to select after EACH instance in a string/substring but I'm not getting the results I wanted.
Input:
<names>
<author>Lio-Po, Gilda D.</author>
<author>Primavera, Jurgenne H.</author>
<author>Cuvin-Aralar, Ma. Lourdes A.</author>
<author>Cruz, E.R.</author>
<author>Catacutan, M.R.</author>
<author>Agbayani, R.F.</author>
</names>
The desired output would be: Lio-Po, G. D., Primavera, J. H., Cuvin-Aralar, M. L. A., Cruz, E. R., Catacutan, M. R., & Agbayani, R. F.
For records with just 2 authors:
<names>
<author>Lio-Po, Gilda D.</author>
<author>Primavera, Jurgenne H.</author>
</names>
The desired output would be: Lio-Po, G. D., & Primavera, J. H.
Thanks in advance. Below is my code with some code taken from Dimitre's.
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="/names/author">
<xsl:choose>
<xsl:when test="count(following-sibling::text()) = 1">
<xsl:text>& </xsl:text>
<xsl:apply-templates/>
</xsl:when>
<xsl:when test="count(following-sibling::text()) != 0">
<xsl:apply-templates/>
<xsl:text>, </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="substring-before(., ',')"/>
<xsl:call-template name="replaceTokenDelims">
<xsl:with-param name="pStr" select="concat(normalize-space(substring-after(., ',')), ' ')"/>
<xsl:with-param name="pToken" select="' '"/>
<xsl:with-param name="pReplacement" select="', '"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replaceTokenDelims">
<xsl:param name="pStr"/>
<xsl:param name="pToken"/>
<xsl:param name="pReplacement"/>
<xsl:if test="$pStr">
<xsl:value-of select="$pReplacement"/>
<xsl:value-of select="substring(substring-before($pStr, $pToken),1,1)"/>
<xsl:call-template name="replaceTokenDelims">
<xsl:with-param name="pStr" select="substring-after($pStr, $pToken)"/>
<xsl:with-param name="pToken" select="$pToken"/>
<xsl:with-param name="pReplacement" select="$pReplacement"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Running the above code gives me the output: Lio-Po, G, D, Primavera, J, H, Cuvin-Aralar, M, L, A, Cruz, E, Catacutan, M, & Agbayani, R
I think I got it!
If tested with the following input:
The desired result is produced:
UPDATED
This one works, I even tested it myself:
And the output for your given XML would be:
If you can live with the last extra comma ;)
APPEND: This is the full java code which I tested and looks ok.