<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>Transformation differences when using a JDOMResult vs. a DOMResult</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>Hi, </FONT>
</P>
<P><FONT SIZE=2>I have a need to transform an XML document into an XSL document, in preparation for a final transformation on an XML document.</FONT></P>
<P><FONT SIZE=2>I have written a test class that transforms a file (empty.xml) using another file (test.xsl). The Document returned is different, and incorrect, if I use a JDOMResult instead of a DOMResult.</FONT></P>
<P><FONT SIZE=2>Using DOMResult returns: </FONT>
<BR><FONT SIZE=2><?xml version="1.0" encoding="UTF-8" standalone="no"?> </FONT>
<BR><FONT SIZE=2><foo:myelement xmlns:foo="<A HREF="http://www.mywebsite.org" TARGET="_blank">http://www.mywebsite.org</A>">content blah blah</foo:myelement> </FONT>
</P>
<P><FONT SIZE=2>Using JDOMResult returns: </FONT>
<BR><FONT SIZE=2><?xml version="1.0" encoding="UTF-8"?> </FONT>
<BR><FONT SIZE=2><foo:myelement xmlns:foo="<A HREF="http://www.mywebsite.org" TARGET="_blank">http://www.mywebsite.org</A>" foo="<A HREF="http://www.mywebsite.org" TARGET="_blank">http://www.mywebsite.org</A>">content blah blah</foo:myelement> </FONT>
</P>
<P><FONT SIZE=2>Notice the extra attribute returned using the JDOMResult (foo="<A HREF="http://www.mywebsite.org" TARGET="_blank">http://www.mywebsite.org</A>"). </FONT>
</P>
<P><FONT SIZE=2>Is there a way to make the JDOMResult not have the extra attribute? </FONT>
</P>
<P><FONT SIZE=2>Thanks in advance,</FONT>
</P>
<P><FONT SIZE=2>Michael</FONT>
</P>
<P><FONT SIZE=2>Example info below</FONT>
</P>
<P><FONT SIZE=2>I am using jdk 1.3.1, xerces 1.3, xalan 2.1.0, and I have downloaded the latest code from cvs.jdom.org this morning. </FONT>
</P>
<P><FONT SIZE=2>Here is my empty.xml file: </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
<BR><FONT SIZE=2><!-- edited with XML Spy v3.5 NT (<A HREF="http://www.xmlspy.com" TARGET="_blank">http://www.xmlspy.com</A>) by Maggie (OGI) --> </FONT>
<BR><FONT SIZE=2><root>empty</root> </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
</P>
<P><FONT SIZE=2>Here is my test.xsl file: </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
<BR><FONT SIZE=2><?xml version="1.0"?> </FONT>
<BR><FONT SIZE=2><xsl:stylesheet version="1.0" xmlns:xsl="<A HREF="http://www.w3.org/1999/XSL/Transform" TARGET="_blank">http://www.w3.org/1999/XSL/Transform</A>"</FONT></P>
<P><FONT SIZE=2> xmlns:foo="<A HREF="http://www.mywebsite.org" TARGET="_blank">http://www.mywebsite.org</A>"> </FONT>
<BR><FONT SIZE=2> <xsl:template match="/"> </FONT>
<BR><FONT SIZE=2> <foo:myelement>content blah blah</foo:myelement> </FONT>
<BR><FONT SIZE=2> </xsl:template> </FONT>
<BR><FONT SIZE=2></xsl:stylesheet> </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
</P>
<P><FONT SIZE=2>Here is my test program: </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
<BR><FONT SIZE=2>// Imported TraX classes </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.TransformerFactory; </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.Transformer; </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.stream.StreamSource; </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.TransformerException; </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.TransformerConfigurationException; </FONT>
<BR><FONT SIZE=2>import javax.xml.transform.dom.DOMResult; </FONT>
</P>
<P><FONT SIZE=2>// Imported java.io classes </FONT>
<BR><FONT SIZE=2>import java.io.IOException; </FONT>
</P>
<P><FONT SIZE=2>// Imported java.net classes </FONT>
<BR><FONT SIZE=2>import java.net.URL; </FONT>
</P>
<P><FONT SIZE=2>// Imported JDOM classes </FONT>
<BR><FONT SIZE=2>import org.jdom.Document; </FONT>
<BR><FONT SIZE=2>import org.jdom.transform.JDOMResult; </FONT>
<BR><FONT SIZE=2>import org.jdom.output.XMLOutputter; </FONT>
</P>
<P><FONT SIZE=2>// Imported Serializer classes </FONT>
<BR><FONT SIZE=2>import org.apache.xalan.serialize.Serializer; </FONT>
<BR><FONT SIZE=2>import org.apache.xalan.serialize.SerializerFactory; </FONT>
</P>
<P><FONT SIZE=2>import org.apache.xalan.templates.OutputProperties; </FONT>
</P>
<P><FONT SIZE=2>/** </FONT>
<BR><FONT SIZE=2> * This class tests the transformation of an XSL file into another XSL file. </FONT>
<BR><FONT SIZE=2> * </FONT>
<BR><FONT SIZE=2> * It compares the differences between using a JDOMResult vs. a DOMResult. </FONT>
<BR><FONT SIZE=2> * </FONT>
<BR><FONT SIZE=2> * To use a DOMResult: java DOM2DOMTest </FONT>
<BR><FONT SIZE=2> * To use a JDOMResult: java DOM2DOMTest JDOM </FONT>
<BR><FONT SIZE=2> */ </FONT>
<BR><FONT SIZE=2>public class DOM2DOMTest </FONT>
<BR><FONT SIZE=2>{ </FONT>
<BR><FONT SIZE=2> public static void main(String[] args) </FONT>
<BR><FONT SIZE=2> throws TransformerException, </FONT>
<BR><FONT SIZE=2> TransformerConfigurationException, </FONT>
<BR><FONT SIZE=2> IOException </FONT>
<BR><FONT SIZE=2> { </FONT>
<BR><FONT SIZE=2> URL xslUrl = new URL("<A HREF="file:test.xsl" TARGET="_blank">file:test.xsl</A>"); </FONT>
<BR><FONT SIZE=2> StreamSource xslDomSource = new StreamSource(xslUrl.openStream()); </FONT>
<BR><FONT SIZE=2> xslDomSource.setSystemId(xslUrl.getPath()); </FONT>
</P>
<P><FONT SIZE=2> URL xmlUrl = new URL("<A HREF="file:empty.xml" TARGET="_blank">file:empty.xml</A>"); </FONT>
<BR><FONT SIZE=2> StreamSource xmlDomSource = new StreamSource(xmlUrl.openStream()); </FONT>
<BR><FONT SIZE=2> xmlDomSource.setSystemId(xmlUrl.getPath()); </FONT>
</P>
<P><FONT SIZE=2> TransformerFactory tFactory = TransformerFactory.newInstance(); </FONT>
<BR><FONT SIZE=2> Transformer transformer = tFactory.newTransformer(xslDomSource); </FONT>
</P>
<P><FONT SIZE=2> if (args.length == 1 && args[0].equalsIgnoreCase("JDOM")) </FONT>
<BR><FONT SIZE=2> { </FONT>
<BR><FONT SIZE=2> outputUsingJDOM(transformer, xmlDomSource); </FONT>
<BR><FONT SIZE=2> } </FONT>
<BR><FONT SIZE=2> else </FONT>
<BR><FONT SIZE=2> { </FONT>
<BR><FONT SIZE=2> useStandardOutput(transformer, xmlDomSource); </FONT>
<BR><FONT SIZE=2> } </FONT>
<BR><FONT SIZE=2> } </FONT>
</P>
<P><FONT SIZE=2> private static void useStandardOutput(Transformer transformer, StreamSource xmlDomSource) </FONT>
<BR><FONT SIZE=2> throws TransformerException, IOException </FONT>
<BR><FONT SIZE=2> { </FONT>
<BR><FONT SIZE=2> DOMResult domResult = new DOMResult(); </FONT>
<BR><FONT SIZE=2> transformer.transform(xmlDomSource, domResult); </FONT>
</P>
<P><FONT SIZE=2> Serializer serializer = SerializerFactory.getSerializer </FONT>
<BR><FONT SIZE=2> (OutputProperties.getDefaultMethodProperties("xml"));</FONT></P>
<P><FONT SIZE=2> serializer.setOutputStream(System.out); </FONT>
<BR><FONT SIZE=2> serializer.asDOMSerializer().serialize(domResult.getNode()); </FONT>
<BR><FONT SIZE=2> } </FONT>
</P>
<P><FONT SIZE=2> private static void outputUsingJDOM(Transformer transformer, StreamSource xmlDomSource) </FONT>
<BR><FONT SIZE=2> throws TransformerException, IOException </FONT>
<BR><FONT SIZE=2> { </FONT>
<BR><FONT SIZE=2> JDOMResult domResult = new JDOMResult(); </FONT>
</P>
<P><FONT SIZE=2> transformer.transform(xmlDomSource, domResult); </FONT>
<BR><FONT SIZE=2> XMLOutputter outputter = new XMLOutputter(); </FONT>
<BR><FONT SIZE=2> outputter.output(domResult.getDocument(), System.out); </FONT>
<BR><FONT SIZE=2> } </FONT>
<BR><FONT SIZE=2>} </FONT>
<BR><FONT SIZE=2>======================================================================== </FONT>
</P>
</BODY>
</HTML>