tags:

views:

65

answers:

3

I'm using XSLT transfer an XML to a different format XML. If there is empty data with the element, it will display as a self-closing, eg. <data />, but I want output it with the closing tag like this <data></data>.

If I change the output method from "xml" to "html" then I can get the <data></data>, but I will lose the <?xml version="1.0" encoding="UTF-8"?> on the top of the document. Is this the correct way of doing this?

Many thanks.

Daoming

+2  A: 

Please, take a look here: MSXML serialisation of empty elements

Rubens Farias
This is a good article. Thank you very much.
Daoming Yang
+1  A: 

Self-closed and explicitly closed elements are exactly the same thing in any regard whatsoever.

Only if somewhere along your processing chain there is a tool that is not XML aware (code that does XML processing with regex, for example), it might make a difference. At which point you should think about changing that part of the processing, instead of the XML generation/serialization part.

Tomalak
+2  A: 

If you want this because you think that self closing tags are ugly, then get over it.

If you want to pass the output to some non-conformant XML Parser that is under control, then use a better parser, or fix the one you are using.

If it is out of your control, and you must send it to an inadequate XML Parser, then do you really need the prolog? If not, then html output method is fine.

If you do need the XML prolog, then you could use the html output method, and prepend the prolog after transformation, but before sending it to the deficient parser.

Alternatively, you could output it as XML with self-closing tags, and preprocess before sending it to your deficient parser with some kind of custom serialisation, using the DOM. If it can't handle self-closing tags, then I'm sure that isn't the only way in which it fails to parse XML. You might need to do something about namespaces, for example.

You could try adding an empty text node to any empty elements that you are outputting. That might do the trick.

Paul Butcher