问题
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