使用 XPath 并检索其值的任何地方,使用normalize-space(YOUREXPR)
,在哪里YOUREXPR
是返回空格的当前表达式。
它将消除尾随和前导空格,并将折叠任何重复的空格。
或者,既然您说“如果 xml 上的任何节点包含空格,则该进程将挂起或出错。” ,您可以使用 完全删除所有空格,translate(YOUREXPR, ' ', '')
这将变成Everyday Italian
。EverydayItalian
如果您还想消除换行符和制表符,您可以使用
translate(YOUREXPR, ' 	

', '')
更新:根据您的评论,您似乎真的想从过多的空间中清除文件。在这种情况下,您应该使用 XSLT。类似以下内容将创建文件副本,其中删除文本节点和属性节点中的所有多余空格(未经测试,但有效,如果您是 XSLT 新手,我建议您通过许多教程或书籍中的任何一个来阅读它):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="normalize-space(.)" />
</xsl:attribute>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)" />
</xsl:template>
</xsl:template>