How get first name initial and last name in XSLT

﹥>﹥吖頭↗ 提交于 2020-01-25 10:05:06

问题


I want yo get the first name initial and last name.

Input :

<root>
  <ele name="Samp Huwani"/>
  <ele name="Gong Gitry"/>
  <ele name="Dery Wertnu"/>
</root>

Output

<names>S Huwani</name>
<names>G Gitry</name>
<names>D Wertnu</name>

Tried Code:

<xsl:template match="root/name">
  <names>
    <xsl:value-of select="@name" />
  </name>
</xsl:template>

I am using XSLT 2.0 . Thank you


回答1:


With the given example, you could use:

<xsl:template match="/root">
    <xsl:copy>
        <xsl:for-each select="ele">
            <name>
                <xsl:value-of select="substring(@name, 1, 1)"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="substring-after(@name, ' ')"/>
            </name>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

However, names often do not conform to the same pattern.


In XSLT 2.0, you could simplify(?) this by using regex, e.g.:

<xsl:value-of select="replace(@name, '^(.{1}).* (.*)', '$1 $2')"/>


来源:https://stackoverflow.com/questions/58264762/how-get-first-name-initial-and-last-name-in-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!