如何在 XSLT 后保留空 XML 标记 - 防止它们从 <B></B> 折叠为 <B/>

2022-09-04 02:07:23

假设我有一个非常简单的XML,带有空标签“B”:

<Root>
  <A>foo</A>
  <B></B>
  <C>bar</C>
</Root>

我目前正在使用XSLT删除一些标签,例如“C”:

<?xml version="1.0" ?>

<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="no" encoding="utf-8" omit-xml-declaration="yes" />

<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

<xsl:template match="C" />

</xsl:stylesheet>

到目前为止还行,但问题是我最终得到了这样的输出:

<Root>
  <A>foo</A>
  <B/>
</Root>

当我真正想要的时候:

<Root>
  <A>foo</A>
  <B></B>
</Root>

有没有办法防止“B”崩溃?

谢谢。


答案 1

好吧,所以这里对我有用:

<xsl:output method="html">

答案 2

试试这个:

<script type="..." src="...">&#160;</script>

您的 HTML 输出将是:

<script type="..." src="..."> </script>

可防止折叠,但会转换为空白区域。它过去对我有用。&#160;


推荐