XSLT 无法处理实体

XSLT can't handle entity

我有一个 XML- 字符串,我正在尝试删除所有空的 XML- 标签和空格。 为此,我正在使用以下 XSL-Sheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="*[not(*) and not(text()[normalize-space()])]"/>
</xsl:stylesheet>

我的输入字符串 XML 例如:

String s = "<main>           <test>123öü aksdjf0192301ß09aasdfg   0</test>            <test> </test>  <test>12031</test>"

             + "\n" +

             "<test>" + "</test>" + "</main>";

对于 XSL 转换,我使用以下 Java 代码:

StringReader reader = new StringReader(xmlContent);
  StringWriter writer = new StringWriter();
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource(
     "style.xsl"));
  transformer.transform(new javax.xml.transform.stream.StreamSource(reader),
     new javax.xml.transform.stream.StreamResult(writer));
  String result = writer.toString();

我的输出是:

<?xml version="1.0" encoding="UTF-8"?><main><test>123öü aksdjf0192301ß09aasdfg   0</test><test>12031</test></main>

这正是我的预期结果,但是当我将 '&' 等实体添加到我的输入字符串时,转换失败。

出现错误:实体名称必须紧跟实体引用中的“&”。

我该如何解决这个问题? XSL 是实现这样的功能的正确原因吗?

我期望这个输出:

 <?xml version="1.0" encoding="UTF-8"?><main><test>123öü aksdjf0192301ß09aasdfg   0</test><test>120>&& | 31</test></main>

有了这个输入:

 String s = "<main>           <test>123öü aksdjf0192301ß09aasdfg   0 >&& |  </test>            <test> </test>  <test>120>&& | 31</test>"

                 + "\n" +

                 "<test>" + "</test>" + "</main>";

& 符号 (&) 本身不是合法字符,您必须将其编码为 &amp;

来自XML Specification

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings &amp; and &lt; respectively.