使用此 XPath 1.0 表达式:
concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
为了验证这一点,进行以下转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=
"concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
"/>
</xsl:template>
</xsl:stylesheet>
当应用于此 XML 文档时:
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>
产生想要的正确结果:
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>