使用 xslt 解析并格式化名称 [英] Using xslt to parse and then format names
问题描述
我正在尝试编写一个 XSLT 样式表,该样式表将处理作者姓名并创建引文的 APA 版本.关于作者姓名的 APA 引文格式:如果姓名是引文的第一个元素,则列出姓氏,然后是姓名首字母.在最后一位作者之前用逗号和与号 (&) 分隔姓名.我在这篇文章中遵循了 Dimitre Novatchev 的解决方案:使用XSLT 在字符串/子字符串中的每个实例之后进行选择,但我没有得到我想要的结果.
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.
输入:
<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>
所需的输出是:Lio-Po, G. D., Primavera, J. H., Cuvin-Aralar, M. L. A., Cruz, E. R., Catacutan, M. R., & Agbayani, R. F.
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.
对于只有 2 个作者的记录:
<names>
<author>Lio-Po, Gilda D.</author>
<author>Primavera, Jurgenne H.</author>
</names>
所需的输出是:Lio-Po, G. D., & Primavera, J. H.
提前致谢.下面是我的代码,其中一些代码来自 Dimitre.
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>
运行上面的代码给我输出:Lio-Po, G, D, Primavera, J, H, Cuvin-Aralar, M, L, A, Cruz, E, Catacutan, M, & Agbayani,R
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
推荐答案
我想我明白了!
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="/names/author">
<xsl:variable name="firstname">
<xsl:value-of select="substring-after(., ', ')"/>
</xsl:variable>
<xsl:if test="position() = last() and position() != 1">
<xsl:text>& </xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="substring($firstname,2,1) = '.'">
<xsl:value-of select="concat(str:tokenize(.,','), ', ')"/>
<xsl:for-each select="str:tokenize($firstname,'.')">
<xsl:value-of select="concat(.,'.')"/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(str:tokenize(.,','), ', ')"/>
<xsl:for-each select="str:tokenize($firstname,' ')">
<xsl:value-of select="concat(substring(.,1,1),'.')"/>
<xsl:if test="position() != last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
如果使用以下输入进行测试:
<names>
<author>Lio-Po, Gilda D.</author>
<author>Cuvin-Aralar, Ma. Lourdes A.</author>
<author>De la Cruz, Juan C.</author>
<author>Cruz, E.R.</author>
<author>Catacutan, M.R.</author>
<author>De los Santos, M.A.</author>
<author>Primavera, Jurgenne H.</author>
</names>
产生了想要的结果:
Lio-Po, G. D., Cuvin-Aralar, M. L. A., De la Cruz, J. C., Cruz, E. R., Catacutan, M. R., De los Santos, M. A., & Primavera, J. H.
这篇关于使用 xslt 解析并格式化名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!