使用xslt解析名称
我正试图编写一个XSLT样式表,它将处理作者的姓名,并将其格式化,如在Google scholar搜索结果中所示 我的目标是遵循上图(突出显示)中所示的名称格式,例如,如果我有以下xml:使用xslt解析名称,xslt,xslt-1.0,Xslt,Xslt 1.0,我正试图编写一个XSLT样式表,它将处理作者的姓名,并将其格式化,如在Google scholar搜索结果中所示 我的目标是遵循上图(突出显示)中所示的名称格式,例如,如果我有以下xml: <names> <author>Naylor, Rosamond L.</author> <author>Goldburg, Rebecca J.</author> <author>
<names>
<author>Naylor, Rosamond L.</author>
<author>Goldburg, Rebecca J.</author>
<author>Primavera, Jurgenne H.</author>
<author>Kautsky, Nils</author>
</names>
我很久以前就在这里发帖了
提前感谢。给出:
XML
<names>
<author>Naylor, R.L.</author>
<author>Goldburg, R. J.</author>
<author>Primavera, J.H.</author>
<author>Kautsky, Nils</author>
</names>
警告:
XSLT1.0中没有大写()函数。上述方法仅适用于显式枚举的大写字符。如果文本包含其他字符(如带变音符号的字符),则将失败
如果这是一个问题,最好使用以下内容:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="names">
<xsl:for-each select="author">
<xsl:for-each select="str:tokenize(substring-after(., ', '),' .')">
<xsl:value-of select="substring(., 1, 1)" />
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-before(., ', ')" />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
,
假设您的处理器支持EXSLT str:tokenize()函数(如果不支持,则必须调用命名的递归模板来进行标记化)。是,请发布代码。(示例XML输入和XSLT)。谢谢
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="names">
<xsl:for-each select="author">
<xsl:variable name="first-names" select="substring-after(., ', ')" />
<xsl:value-of select="translate($first-names, translate($first-names, $upper-case, ''), '')"/>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-before(., ', ')" />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
RL Naylor, RJ Goldburg, JH Primavera, N Kautsky
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="names">
<xsl:for-each select="author">
<xsl:for-each select="str:tokenize(substring-after(., ', '),' .')">
<xsl:value-of select="substring(., 1, 1)" />
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:value-of select="substring-before(., ', ')" />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>