这在 XPath 1.0 中是可能的,假设:
如果你对这两个都没问题,那么我们开始吧:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text"/>
<xsl:template match="/">
<xsl:variable name="name" select="' Ann: blah blah '"/>
<xsl:variable name="cname"
select="normalize-space(substring-before($name, ':'))"/>
<xsl:variable name="after-first"
select="normalize-space(substring-after($cname, ' '))"/>
<!-- first name -->
[<xsl:value-of select="substring-before(concat($cname, ' '), ' ')"/>]
<!-- middle name -->
[<xsl:value-of select="concat(
substring(substring-before($after-first, ' '), 1,
number(contains($after-first, ' ')) *
string-length($after-first)),
substring('', 1,
number(not(contains($after-first, ' '))) *
string-length('')))"/>]
<!-- last name -->
[<xsl:value-of select="concat(
substring(substring-after($after-first, ' '), 1,
number(contains($after-first, ' ')) *
string-length($after-first)),
substring($after-first, 1,
number(not(contains($after-first, ' '))) *
string-length($after-first)))"/>]
</xsl:template>
</xsl:stylesheet>
使用:
<xsl:variable name="name" select="' Ann Q. Smith : blah blah '"/>
输出:
[Ann]
[Q.]
[Smith]
使用:
<xsl:variable name="name" select="' Ann Smith : blah blah '"/>
输出:
[Ann]
[]
[Smith]
使用:
<xsl:variable name="name" select="' Ann: blah blah '"/>
输出:
[Ann]
[]
[]