使用 XSLT 删除元素删除留下的空格

Remove whitespace left by element removal using XSLT

我正在使用 XSLT 空模板从我的 XML 中删除一个节点。即 <xsl:template match="note"/>。它工作正常,但我在它两侧的节点之间留下了一条空线。我想保留一般的空白并缩进输出但只是摆脱这个空白行。有没有办法做到这一点,或者我是否必须 运行 第二次通过文档并在空白行上以某种方式匹配?

...
<name>Keep whitespace</name>
                                       <-- where the <note/> element was
<favourite-color>keep white space</favourite-color>
...

理想情况下我会:

...
<name>Keep whitespace</name>
<favourite-color>keep white space</favourite-color>
...

在许多情况下,在解析时去除白色 space 并在序列化时缩进输出就足够了:<xsl:strip-space elements="*"/><xsl:output indent="yes"/>.

或者您可以使用 <xsl:template match="note | text()[not(normalize-space()) and preceding-sibling::node()[1][self::note]]"/>.