Dropping namespace attribute during transform
Added by Anonymous over 16 years ago
Legacy ID: #5019042 Legacy Poster: Scott Parkey (s_parkey)
When I have the following XML file named samplein.xml: <?xml version="1.0" encoding="UTF-8"?> <Document xmlns="namespace" xmlns:xsi="http://www.namespacexsi.com"/> and I run the following Java program: import java.io.File; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import net.sf.saxon.TransformerFactoryImpl; public class SaxonTest { public static void main(String[] args) { try { // Read an XML file to a DOM DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder db = factory.newDocumentBuilder(); org.w3c.dom.Document doc = db.parse(new File("samplein.xml")); // Output a DOM to an XML file TransformerFactory tFactory = new TransformerFactoryImpl(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); FileOutputStream fos = new FileOutputStream("sampleout.xml"); transformer.transform(source, new StreamResult(fos)); } catch(Exception e) { e.printStackTrace(); } } } it outputs the following XML file: <?xml version="1.0" encoding="UTF-8"?> <Document xmlns:xsi="http://www.namespacexsi.com"/> Notice that the xmlns="namespace" attribute is now missing. But the code above looks like it should do nothing but read an XML as a DOM and output the DOM back to an XML file. When I change the code to use a different TransformerFactory than Saxon, it produces the same file as it was given. So why does Saxon remove the namespace attribute?
Replies (3)
Please register to reply
RE: Dropping namespace attribute during trans - Added by Anonymous over 16 years ago
Legacy ID: #5019069 Legacy Poster: Michael Kay (mhkay)
It looks suspiciously like this bug http://sourceforge.net/tracker/index.php?func=detail&aid=1563935&group_id=29872&atid=397617 which was in Saxon 8.8 and (supposedly) cleared in 8.9. Could I check which version you are running?
RE: Dropping namespace attribute during trans - Added by Anonymous over 16 years ago
Legacy ID: #5019146 Legacy Poster: Scott Parkey (s_parkey)
I had originally run it using an older version (8 point something) but I tried it using the JAR files given in Saxon-B 9.0 and it appears to give the same behavior.
RE: Dropping namespace attribute during trans - Added by Anonymous over 16 years ago
Legacy ID: #5019180 Legacy Poster: Michael Kay (mhkay)
You need to set factory.setNamespaceAware(true) on the DocumentBuilderFactory, otherwise namespaces are not retained in the DOM model.
Please register to reply