XSLT:删除多余的空白字符保留节点 [英] XSLT: Remove excess whitespace characters preserving nodes

查看:30
本文介绍了XSLT:删除多余的空白字符保留节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是这个.我有一个在很多地方使用的转换文档,并且一般处理很多小的格式转换.在一种特定情况下,我需要从结果中删除空格.输出类似于:

So my problem is this. I have a transform document which is used in many places, and generically handles a lot of small formatting transforms. In one specific case, I need to remove whitespace from the result. The output looks something like:

'\n      <I>某事</I>带有上标注释的非常重要的内容<SUP>1</SUP>\n      '

'\n         <I>Something</I>Very Significant With A Superscript Note<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()"这样的通用模板,因为它会干扰转换的一般处理.似乎我在这里遗漏了一些明显的东西.

but the subnodes are stripped from the output. I have to be very careful not to set up any universal templates like 'text()' as it'll interfere with the general processing of the transform. It seems like I'm missing something obvious here.

尝试按照 Stefan-Hegny 的建议编写身份转换.

Tried writing an identity transform as suggested by 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>

这解决了我的问题,即删除标签最高级别的空格和换行符,然后允许转换正常进行.对这个含糊不清的问题表示歉意,并感谢您的帮助.

This solved my issue, which was to remove whitespace and newlines at the highest level of the tag, then allow transforms to proceed normally. Apologies for the murkily-constructed question, and thanks for your assistance.

编辑第二个:'translate' 的使用不像我预期的那样工作,它逐个字符地工作.我使用了替换子字符串的转换.

EDIT the second: The use of 'translate' doesn't work as I expected, it works character by character. I used a transform that replaces substrings instead.

推荐答案

我有两个选择:

如果 \n 不是字面意思,则执行以下操作:

If the \n it not literal then do this:

<xsl:template match="text()[ancestor-or-self::no_whitespace]">
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

清除 no_whitespace 标记处和下方的所有空白.

to clean up all white space at and below the no_whitespace tag.

如果 \n 是字符串中的文字,那么去掉 \n 会变得更复杂一些.使用这个:

If the \n is a literal in the string then its get a bit more complex to get rid of the \n. Use this:

<xsl:template name="strip_newline">
    <xsl:param name="string"/>
    <xsl:value-of select="substring-before($string,'\n')"/>
    <xsl:variable name="rhs" select="substring-after($string,'\n')"/>
    <xsl:if test="$rhs">
        <xsl:call-template name="strip_newline">
            <xsl:with-param name="string" select="$rhs"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template match="text()[ancestor-or-self::no_whitespace]">
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

<xsl:template match="text()[ancestor-or-self::no_whitespace][contains(.,'\n')]">
    <xsl:variable name="cleantext">
        <xsl:call-template name="strip_newline">
            <xsl:with-param name="string" select="."/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="normalize-space($cleantext)"/>
</xsl:template>

在这两种情况下,我都假设您在 xsl 的其他地方已经有了一个身份模板:

In both cases, I'm assuming you already have an identity template in place elsewhere in you xsl:

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

这篇关于XSLT:删除多余的空白字符保留节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆