You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by bu...@apache.org on 2003/01/30 18:50:39 UTC

DO NOT REPLY [Bug 16613] New: - I cannot use dummy transform technique to serialize a document with namespaces

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16613>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16613

I cannot use dummy transform technique to serialize a document with namespaces

           Summary: I cannot use dummy transform technique to serialize a
                    document with namespaces
           Product: XalanJ2
           Version: 2.4
          Platform: PC
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Major
          Priority: Other
         Component: javax.xml
        AssignedTo: xalan-dev@xml.apache.org
        ReportedBy: rsakala@digev.com
                CC: rsakala@digev.com


I am creating a DOM document with JAXP interface with namespace elements and 
attributes. I am seeing different behavior with different versions of Xerces 
versions; None of them except version 2.0.1 seems to be correct.

With version 2.3.0 and 2.2.1:
If I use XMLSerializer, I do not get the namespace attributes.
If I use the DOM3 procedure, I get the namespace attributes; but all of the 
elements have the namespace attributes unnecessarily instead of having it once 
on the root node.
If I use a dummy XSLT transformer with DOM source and Stream result, I do not 
get any namespace nodes.

With version 2.0.1:
If I use XMLSerializer, I get the namespace attributes properly.
If I use a dummy XSLT transformer with DOM source and Stream result, I do not 
get any namespace nodes.

I prefer to get this code with work with dummy XSLT transformer so that I do 
not have to use native Xerces API. Instead, my code will only use JAXP 
interface and everything happens behind the scenes.

I have attached the code I wrote below.

Here is exact result I am seeing with different xerces parsers.

With Xerces2.0.1.jar, I see the following in System.out
_________________
Serialize With Dummy Transform:

<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement><mspm:element mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>

Serialize With FAQ2 Help: 

<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement xmlns:mspm="http://rsakala"><mspm:element 
xmlns:mspm2="http://rsakala2" mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>
_________________

With Xerces 2.2.1.jar, I see the following in System.out
_________________

Serialize With Dummy Transform:
<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement><mspm:element mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>

Serialize With FAQ2 Help: 
<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement><mspm:element mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>

Serialize With FAQ3 Help: 
<?xml version="1.0"?>
<mspm:rootelement xmlns:mspm="http://rsakala"><mspm:element 
xmlns:mspm="http://rsakala" xmlns:mspm2="http://rsakala2" 
mspm2:mspm2attr2="attrvalue2" mspm:mspmattr1="attrvalue1"/></mspm:rootelement>
_________________

With Xerces 2.3.0.jar, I see the following in System.out
_________________
Serialize With Dummy Transform:

<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement><mspm:element mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>


Serialize With FAQ2 Help: 

<?xml version="1.0" encoding="UTF-8"?>
<mspm:rootelement><mspm:element mspm2:mspm2attr2="attrvalue2" 
mspm:mspmattr1="attrvalue1"/></mspm:rootelement>


Serialize With FAQ3 Help: 

<?xml version="1.0"?>
<mspm:rootelement xmlns:mspm="http://rsakala"><mspm:element 
xmlns:mspm="http://rsakala" xmlns:mspm2="http://rsakala2" 
mspm2:mspm2attr2="attrvalue2" mspm:mspmattr1="attrvalue1"/></mspm:rootelement>
_________________


Source code for the java class:
______________
import java.io.Writer;

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 org.apache.xerces.dom3.DOMImplementationRegistry;
import org.apache.xml.serialize.LineSeparator;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.DOMWriter;

public class TextXML {

	public static void main(String[] args) {
		try {
			Document doc = buildXMLDoc();
			serializeWithDummyTransform(doc);
			serializeWithDOM2FAQHelp(doc);
			serializeWithDOM3FAQHelp(doc);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private static Document buildXMLDoc() throws Exception {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
();
		DocumentBuilder db = dbf.newDocumentBuilder();
		dbf.setNamespaceAware(true);
		dbf.setValidating(false);
		Document doc = db.newDocument();
		Element elem = doc.createElementNS
("http://rsakala", "mspm:rootelement");
		doc.appendChild(elem);
		Element elem2 = doc.createElementNS
("http://rsakala", "mspm:element");
		elem2.setAttributeNS
("http://rsakala", "mspm:mspmattr1", "attrvalue1");
		elem2.setAttributeNS
("http://rsakala2", "mspm2:mspm2attr2", "attrvalue2");
		elem.appendChild(elem2);

		return doc;

	}

	private static void serializeWithDummyTransform(Document doc)
		throws Exception {
		System.out.println("Serialize With Dummy Transform:");
		TransformerFactory trFactory = TransformerFactory.newInstance();
		Transformer tr = trFactory.newTransformer();
		javax.xml.transform.dom.DOMSource domSrc =
			new javax.xml.transform.dom.DOMSource(doc);
		Writer writer = new java.io.StringWriter();
		javax.xml.transform.stream.StreamResult strRes =
			new javax.xml.transform.stream.StreamResult(writer);
		tr.transform(domSrc, strRes);
		System.out.println(writer.toString());
	}

	private static void serializeWithDOM2FAQHelp(Document doc) throws 
Exception {
		System.out.println("Serialize With FAQ2 Help: ");
		OutputFormat format = new OutputFormat(doc);
		format.setLineSeparator(LineSeparator.Windows);
		format.setIndenting(true);
		format.setLineWidth(0);
		format.setPreserveSpace(true);
		Writer writer = new java.io.StringWriter();
		XMLSerializer serializer = new XMLSerializer(writer, format);
		serializer.asDOMSerializer();
		serializer.serialize(doc);
		System.out.println(writer.toString());
	}

	private static void serializeWithDOM3FAQHelp(Document doc) throws 
Exception {
		System.out.println("Serialize With FAQ3 Help: ");

		System.setProperty(
			DOMImplementationRegistry.PROPERTY,
			"org.apache.xerces.dom.DOMImplementationSourceImpl");
		DOMImplementationRegistry registry = 
DOMImplementationRegistry.newInstance();

		DOMImplementationLS impl =
			(DOMImplementationLS) registry.getDOMImplementation("LS-
Load");

		DOMWriter writer = impl.createDOMWriter();
		writer.writeNode(System.out, doc);
	}
}
______________