Goeff,
I tried two things. The first was to run your input XML through the
identity transform, which will essentially use the same code underneath,
but not use DOM at all.
Here is the code I ran:
static void case3() throws TransformerException, IOException {
final javax.xml.transform.TransformerFactory tFactory;
tFactory = new org.apache.xalan.processor.TransformerFactoryImpl();
final javax.xml.transform.Transformer transformer;
transformer = tFactory.newTransformer();
StringWriter sw = new StringWriter();
StringReader sr = new StringReader(
"<?xml version='1.0' ?>\n" +
"<c:a xmlns:b='http://somenamespace.com/'\n"+
" xmlns:c='http://othernamespace.com/'\n"+
" b:d='e'/>");
StreamResult strmrslt = new StreamResult(sw);
StreamSource strmsrc = new StreamSource(sr);
transformer.setOutputProperty("method","xml");
transformer.setOutputProperty("indent","yes");
transformer.setOutputProperty("standalone","no");
transformer.transform(strmsrc, strmrslt);
sw.flush();
String out = sw.toString();
sw.close();
System.out.println("=================================");
System.out.println(out);
System.out.println("=================================");
}
Here is the output that I got:
=================================
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<c:a xmlns:b="http://somenamespace.com/"
xmlns:c="http://othernamespace.com/" b:d="e"/>
=================================
Then I tried with Xerces as the creator of the DOM.
public static void case4()
throws SAXException, IOException, ParserConfigurationException {
javax.xml.parsers.DocumentBuilderFactory dfactory;
// dfactory =
// javax.xml.parsers.DocumentBuilderFactory.newInstance();
dfactory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
dfactory.setNamespaceAware(true);
final org.w3c.dom.Document document;
{
StringReader sr =
new StringReader(
"<c:a xmlns:b='http://somenamespace.com/' "+
" xmlns:c='http://othernamespace.com/' b:d='e'/>");
StreamSource strmsrc = new StreamSource(sr);
InputSource is = new InputSource(sr);
javax.xml.parsers.DocumentBuilder dBuilder =
dfactory.newDocumentBuilder();
document = dBuilder.parse(is);
}
final StringWriter sw = new StringWriter();
{
java.util.Properties xmlProps =
OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
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("=================================");
}
Whether the factory (dfactory) was set to be namespace aware, or not, made
no difference in what I got. The same output in all cases. Namespace nodes
were not lost.
I don't know how you get your DOM document, but I think that is where the
problem is.
- Brian
- - - - - - - - - - - - - - - - - - - -
Brian Minchau
XSLT Development, IBM Toronto
e-mail: [EMAIL PROTECTED]
"If the lion purrs, it is only because it is saving you for dessert." - My
wife
Geoffrey Shuetrim
<[EMAIL PROTECTED]
> To
xalan
06/02/2005 05:21 <[email protected]>
AM cc
Subject
Please respond to Losing namespace declarations for
geoff namespaces that are used only on
attributes (eg xlink) when using
org.apache.xml.serializer
The subjects says most of it. I am trying to serialize the results of
stylesheet transformations and some xmlns declarations are going
missing.
For example, if I am serializing the following markup:
<c:a
xmlns:b="http://somenamespace.com/"
xmlns:c="http://othernamespace.com/"
b:d="e"/>
then I get the following:
<c:a
xmlns:c="http://othernamespace.com/"
b:d="e"/>
The serialize method that I have written is:
public void serialize(
OutputStream outputStream,
Document document)
throws Exception {
java.util.Properties xmlProps =
OutputPropertiesFactory.getDefaultMethodProperties("xml");
xmlProps.setProperty("indent", "yes");
xmlProps.setProperty("standalone", "no");
Serializer serializer =
SerializerFactory.getSerializer(xmlProps);
serializer.setOutputStream(outputStream);
serializer.asDOMSerializer().serialize(document);
}
The document being serialized has always been parsed by a namespace
aware DOM builder or created by a transformer applied to a namespace
aware DOM and created from a stylesheet parsed in using a namespace
aware DOM builder.
The nearest archived message that I have come across is:
http://mail-archives.apache.org/mod_mbox/xml-security-dev/200409.mbox/%
[EMAIL PROTECTED]
but that relates to XMLSerializer.
Any guidance on what is going on would be very welcome!
Geoff Shuetrim