Xml XSL在空格后显示节点的第一个字母(如果存在空格)
我的XML中有一个名为的节点,如果中间首字母存在,则该节点包含中间首字母。(提供XML的数据库没有中间的初始字段) 示例:Xml XSL在空格后显示节点的第一个字母(如果存在空格),xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我的XML中有一个名为的节点,如果中间首字母存在,则该节点包含中间首字母。(提供XML的数据库没有中间的初始字段) 示例: <billing-firstname>Nicholas M.</billing-firstname> <billing-firstname>Timothy</billing-firstname> N. M. T. <xsl:stylesheet version="2.0" xmlns:xsl="http://ww
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
N. M.
T.
<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>
我已经知道如何捕捉节点的第一个字符,只是不知道如何将其拆分为第一个首字母,然后再拆分为中间首字母(如果存在)
<xsl:value-of select="substring(billing-firstname,1,1)" />
如蒙协助,将不胜感激
-Nick因为您使用的是XSLT 2.0(与XPath 2.0一起使用),所以您可以使用
for
,标记化
,子字符串
,concat
,以及字符串连接
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>
<doc>
<billing-firstname>N. M.</billing-firstname>
<billing-firstname>T.</billing-firstname>
</doc>
尼古拉斯M。
提摩太
XSLT2.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>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
</doc>
<doc>
<billing-firstname>N. M.</billing-firstname>
<billing-firstname>T.</billing-firstname>
</doc>
N.M。
T
此XSLT 2.0转换:
<billing-firstname>Nicholas M.</billing-firstname>
<billing-firstname>Timothy</billing-firstname>
N. M.
T.
<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>
谢谢你的回复。今天晚些时候,我将在我的项目中对此进行测试。(请继续关注)我已经在一个循环中了,运行它会给我在我编码的表中选择的每个数据项/列的第一个字符。