I. Here is a simple and natural XSLT 2.0 solution:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*/*"> <xsl:value-of separator=", " select= "substring-before(., ',') , for $n in tokenize(substring-after(., ','), '\s')[.] return substring($n, 1,1) "/> <xsl:text>
</xsl:text> </xsl:template> </xsl:stylesheet>
When this conversion is applied to the following XML document:
<t xmlns:dc="some:dc"> <dc:creator >Friend, Natasha</dc:creator> <dc:creator>Tolkien, JRR</dc:creator> </t>
the desired, correct result is output:
Friend, N Tolkien, J., R., R.
II. XSLT 1.0 Solution :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <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:text>
</xsl:text> </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> </xsl:stylesheet>
When this conversion is applied to the same XML document (above), the same correct result is obtained again :
Friend, N Tolkien, J., R., R.