使用 xslt 删除 xml 中的无效字符

removing invalid characters in xml with xslt

在 XML 文件中,我在源代码中看到了这个: &lt;#&gt; 这会导致另一个应用程序出现问题,该应用程序将其视为 <#>

我正在使用 XSLT2.0,我尝试对从 # 到 div 的任何内容进行替换,以完全删除带有空字符串的 &lt;#&gt;。 我通过将结构放入变量并替换来完成此操作。

替换功能的结果是我也丢失了所有其他元素。 欢迎提出任何建议。 输入可能如下所示:

`<html>
 <body>
   <p>&lt;#&gt;This is just a test</p>
 </body>
</html>`

但它也可以看起来像这样:

`<html>
 <body>
   &lt;#&gt;<p>This is just a test</p>
 </body>
</html>`

想要输出的是:

`<html>
 <body>
   <p>This is just a test</p>
 </body>
</html>`

我试过的XSL就是这个,它删除了所有元素。我确实看到我是在副本上这样做的,所以这可能是错误的...:[=​​17=]

`<xsl:template name="body">
  <xsl:copy-of select="replace($bodycontent, '#', 'div /')" />
 </xsl:template name="body">

   <xsl:variable name="bodycontent">
    <xsl:apply-templates select="/newsMessage/itemSet/newsItem/contentSet/inlineXML/h:html/h:body/h:section/h:p" />
    <p class="txt-ind">
        <xsl:value-of select="//rightsInfo/copyrightHolder/name" />
    </p>
</xsl:variable>`

如果你使用

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

<xsl:template match="text()">
    <xsl:value-of select="replace(., '&lt;#&gt;', '')"/>
</xsl:template>

然后将删除所有出现的这些字符,在线 http://xsltransform.net/gWvjQfu