I have pinpointed what I think the problem with the Xalan serializer is:
I run the following code:
[snip]
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xml.serializer.OutputPropertiesFactory;
import org.apache.xml.serializer.Serializer;
import org.apache.xml.serializer.SerializerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class TestXalanSerializer {
/**
*
*/
public TestXalanSerializer() {
super();
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
try {
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
DocumentBuilderFactory dfactory =
DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
Document document = dBuilder.newDocument();
Element root =
document.createElementNS("http://somenamespace.com/","c:a");
document.appendChild(root);
root.setAttributeNS("http://othernamespace.com/","b:d","e");
StringWriter sw = new StringWriter();
java.util.Properties xmlProps =
OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
Serializer serializer =
SerializerFactory.getSerializer(xmlProps);
serializer.setWriter(sw);
serializer.asDOMSerializer().serialize(document);
sw.flush();
String out = sw.toString();
sw.close();
System.out.println("=================================");
System.out.println(out);
System.out.println("=================================");
sw = new StringWriter();
org.apache.xml.serialize.OutputFormat format = new
org.apache.xml.serialize.OutputFormat("xml", "UTF-8", true);
org.apache.xml.serialize.XMLSerializer output = new
org.apache.xml.serialize.XMLSerializer(sw, format);
output.setNamespaces(true);
output.serialize(document);
sw.flush();
out = sw.toString();
sw.close();
System.out.println("=================================");
System.out.println(out);
System.out.println("=================================");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.exit(0);
}
}
[snip]
and the output is (Xalan serializer first):
=================================
<?xml version="1.0" encoding="UTF-8"?>
<c:a xmlns:c="http://somenamespace.com/" b:d="e"/>
=================================
=================================
<?xml version="1.0" encoding="UTF-8"?>
<c:a xmlns:c="http://somenamespace.com/"
xmlns:b="http://othernamespace.com/" b:d="e"/>
=================================
Note the missing namespace declaration for the attribute's namespace.
The problem only occurs for attributes added to a dom using the
setAttributeNS method on DOM documents.
I will check if this is a problem with the latest Xalan code (This
reported result was from the 2.6 release) tomorrow.
Cheers
Geoff Shuetrim