Attribute namespaces disappearing
Added by Anonymous over 15 years ago
Legacy ID: #6872486 Legacy Poster: Geoff Gibbs (geoffgibbs)
When using Saxon's DOMSource and transformer any namespaced attributes in my document are being output without a prefix, and therefore in no namespace. The code I am using in my Unit test is : TransformerFactory tf = TransformerFactory.newInstance(); Transformer bumblebee = tf.newTransformer(); DOMSource src = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); bumblebee.transform(src, result); String outcome = writer.toString(); This code worked OK with the default transfomer, Xalan I think, but as soon as I started using Saxon it started to fail. Do I have to enable namespace aware somewhere?
Replies (6)
Please register to reply
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6872884 Legacy Poster: Michael Kay (mhkay)
I've got a test case for this and it's working fine. The fact that I have a test case might mean there was once a bug, but if so, I can't track it down. What release are you using? And how do you build the DOM? It's important to call setNamespaceAware(true) on the DocumentBuilderFactory if you use one. If you still think there's a problem, could you please supply a self-contained JUnit test? It might also be relevant what DOM implementation you are using. A lot of bugs with DOM, especially with namespaces, arise because of differences between one DOM implementation and another. Michael Kay http://www.saxonica.com/
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6885350 Legacy Poster: Geoff Gibbs (geoffgibbs)
I have tried a few different versions, I started with 9.1.0.6, then 9.1.0.5 and 8.8.0.7. All with the same result. Running the following code reproduces the error for me: import java.io.StringWriter; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import net.sf.saxon.TransformerFactoryImpl; import org.w3c.dom.Document; import org.w3c.dom.Element; public class AttributeNamespaces { public static void main(String... args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // Document Builder Factory is com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl // from JRE 1.6.0_07 dbf.setNamespaceAware(true); Document doc = dbf.newDocumentBuilder().newDocument(); String ns = "http://www.world-of-geoff.co.uk/saxon/test"; Element root = doc.createElementNS(ns, "root"); doc.appendChild(root); root.setAttributeNS(ns, "test1", "value1"); root.setAttributeNS(ns, "test2", "value2"); Transformer t = new TransformerFactoryImpl().newTransformer(); StringWriter sw = new StringWriter(); t.transform(new DOMSource(doc), new StreamResult(sw)); // is outputting: // <?xml version="1.0" encoding="UTF-8"?><root xmlns="http://www.world-of-geoff.co.uk/saxon/test" test1="value1" test2="value2"/> System.out.println(sw.toString()); } }
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6885764 Legacy Poster: Michael Kay (mhkay)
DOM will never cease to amaze me. It really allows an attribute to have a namespace URI but no prefix? Yes, looking at the spec, it seems it does. As a matter of interest, what happens if you try and serialize this using the DOM's own serializer? I'll see if this can be supported without too much extra cost. Meanwhile, please give the attribute a prefix.
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6886032 Legacy Poster: Michael Kay (mhkay)
Looking at this, there are so many ways of creating non-well-formed documents when you construct them programmatically, I don't think it's the task of an identity transformer to make them well-formed. For example, it's possible to create attributes that have both a prefix and a URI where the containing element actually binds the prefix to a different URI. I think I'm going to take the approach that if the input isn't well formed, then the output won't be well-formed either. I don't think Saxon should be incurring a lot of extra run-time cost to deal with arbitrary ill-formed input, and in any case, the combinations possible are so vast that it's an impossible task testing them all.
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6886451 Legacy Poster: Geoff Gibbs (geoffgibbs)
The output of the standard serializer is: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns:ns0="http://www.world-of-geoff.co.uk/saxon/test" ns0:test1="value1" xmlns:ns1="http://www.world-of-geoff.co.uk/saxon/test" ns1:test2="value2" xmlns="http://www.world-of-geoff.co.uk/saxon/test"/>
RE: Attribute namespaces disappearing - Added by Anonymous over 15 years ago
Legacy ID: #6886829 Legacy Poster: Michael Kay (mhkay)
Thanks. I think I shall stick with the decision for the time being that says Saxon's behaviour when given a non-well-formed DOM is undefined - it will not in general try to repair it, for example by inventing prefixes.
Please register to reply