XSL 在 Space 之后显示节点的第一个字母(如果 space 存在)
XSL Display first letter of Node after a Space (If a space exists)
我的 XML 中有一个节点用于 名字 ,如果中间名存在,它包括中间名。 (提供 XML 的数据库没有中间名首字母字段)。
例子:
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
我希望能够仅将其显示为首字母。
输出示例:
N. M.
T.
我已经知道如何获取节点的第一个字符,只是不知道如何将它拆分为第一个首字母然后是中间首字母如果它存在。
<xsl:value-of select="substring(billing-firstname,1,1)" />
如有任何帮助,我们将不胜感激。
-尼克
由于您使用的是 XSLT 2.0(带有 XPath 2.0),因此您可以结合使用 for
、tokenize
、substring
、concat
和 string-join
...
string-join(for $name in tokenize(normalize-space(),'\s') return concat(substring($name,1,1),'.'),' ')
示例:
XML 输入
<doc>
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="billing-firstname">
<xsl:copy>
<xsl:value-of select="string-join(for $name in tokenize(normalize-space(),'\s') return concat(substring($name,1,1),'.'),' ')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出
<doc>
<billing-firstname>N. M.</billing-firstname>
<billing-firstname>T.</billing-firstname>
</doc>
此 XSLT 2.0 转换:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="billing-firstname">
<xsl:for-each select="tokenize(., ' ')">
<xsl:value-of select="concat(substring(.,1,1), '. ')"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
当应用于提供的 XML 时(包装在单个顶部元素中以使其格式正确 XML 文档):
<t>
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
</t>
产生想要的正确结果:
N. M.
T.
我的 XML 中有一个节点用于 名字 ,如果中间名存在,它包括中间名。 (提供 XML 的数据库没有中间名首字母字段)。
例子:
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
我希望能够仅将其显示为首字母。
输出示例:
N. M.
T.
我已经知道如何获取节点的第一个字符,只是不知道如何将它拆分为第一个首字母然后是中间首字母如果它存在。
<xsl:value-of select="substring(billing-firstname,1,1)" />
如有任何帮助,我们将不胜感激。
-尼克
由于您使用的是 XSLT 2.0(带有 XPath 2.0),因此您可以结合使用 for
、tokenize
、substring
、concat
和 string-join
...
string-join(for $name in tokenize(normalize-space(),'\s') return concat(substring($name,1,1),'.'),' ')
示例:
XML 输入
<doc>
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="billing-firstname">
<xsl:copy>
<xsl:value-of select="string-join(for $name in tokenize(normalize-space(),'\s') return concat(substring($name,1,1),'.'),' ')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出
<doc>
<billing-firstname>N. M.</billing-firstname>
<billing-firstname>T.</billing-firstname>
</doc>
此 XSLT 2.0 转换:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="billing-firstname">
<xsl:for-each select="tokenize(., ' ')">
<xsl:value-of select="concat(substring(.,1,1), '. ')"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
当应用于提供的 XML 时(包装在单个顶部元素中以使其格式正确 XML 文档):
<t>
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
</t>
产生想要的正确结果:
N. M.
T.