Automatic replacement of closing tags
843834Dec 23 2009 — edited Jul 20 2010Hello all,
I've noticed that, when I use the TransformerFactory to create a new Transformer, the start and end tags of an element that contains no text are automatically replaced by a self-closing tag. More specifically, if I use the following code
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new StreamSource(new FileInputStream(xsltPath))
);
to load an XSLT file containing the following snippet
<script src="..."></script>
and apply it to some XML file, the above HTML code snippet is replaced in the result document by
<script src="..." />
For some reason, browsers do not accept this script element; the script element needs to have an explicit opening and closing tag. I've managed to solve this problem by adding some random characters between the opening and closing tag, so the XML parser will not feel the need to replace the tags by a self-closing tag. But as far as I know, the XML specification reads that "The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag." So, it should not illegal to have an empty element with a start and closing tag in XML.
I would like to find out how I can solve this matter more elegantly. I've set the "method" output property of the Transformer object to "html", but that did not have any effect:
transformer.setOutputProperty(OutputKeys.METHOD, "html");
I've also tried replacing the code by
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(
new SAXSource(new InputSource(FileInputStream(xsltPath)))
);
But that also had no effect. But apparently, you can set certain features and properties of the XMLReader instance used to parse the XSLT file; however, I did not manage to find any relevant features or properties relevant to my problem.
Has anyone encountered this problem before? Is there some way to configure the XMLReader (or some other object) in order to avoid this automatic replacement?
Kind regards,
William
Edited by: darth_willy on Dec 23, 2009 3:05 AM