XSLT can't handle entity



I have a XML- String and i'am trying to remove all empty XML-Tags and whitespaces. For that i'am using the following XSL-Sheet:



<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR">
<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>


My input String XML is for example:



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

+ "\n" +

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


For the XSL Transformation i'am using the following Java code:



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();


My Output is:



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


That's exactly my expected result but when i add an entity for example '&' to my Input string the transformation fails.


With the error: The entity name must immediately follow the '&' in the entity reference.


How can i solve that Problem? And is XSL the correct why to implement a functionality like this?


I expect this Output:



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


With this Input:



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

+ "\n" +

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

No comments:

Post a Comment