所以我的问题是这样的。我有一个在许多地方使用的变换文档,通常处理很多小格式变换。在一个特定情况下,我需要从结果中删除空格。输出看起来是这样的:XSLT:删除多余的空白字符保留节点
“\ n <我>的东西</I>非常显着的有上标注< SUP> 1 </SUP> \ n '
我已经试过上的变化:
<xsl:template match="no_whitespace">
<xsl:variable name="result">
<xsl:apply-templates/>
</xsl:variable>
<xsl:copy-of select="normalize-space($result)"/>
</xsl:template>
但子节点从输出中剥离。我必须非常小心,不要设置像'text()'这样的通用模板,因为它会干扰变换的一般处理。似乎我在这里错过了一些明显的东西。
编辑:试图写作由Stefan Hegny建议的身份转换。
<xsl:template match="title_full">
<xsl:apply-templates mode="stripwhitespace"/>
</xsl:template>
<xsl:template match="text()" mode="stripwhitespace">
<xsl:value-of select="normalize-space(translate(., '\n', ''))"/>
</xsl:template>
<xsl:template match="/ | @* | *" mode="stripwhitespace">
<xsl:apply-templates select="."/>
</xsl:template>
这解决了我的问题,即在标记的最高级别删除空格和换行符,然后允许转换正常进行。对于这个虚构的问题抱歉,并感谢您的帮助。
编辑第二:'translate'的使用不像我预期的那样工作,它按字符工作。我使用了一种替换子字符串的转换。
请给XML输入的reprsentative例子。 –
我应该补充说,这个例子是我正在使用的标题xml的一个例子,但是转换也能够接收各种大段的文本,包含多个段落, – Snow