[jdom-interest] Transformation differences when using a JDOMResult vs. a DOMResul
t
Sexton Michael
msexton at olympusgroup.com
Wed Jun 6 11:07:22 PDT 2001
Hi,
I have a need to transform an XML document into an XSL document, in
preparation for a final transformation on an XML document.
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.
Using DOMResult returns:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<foo:myelement xmlns:foo="http://www.mywebsite.org">content blah
blah</foo:myelement>
Using JDOMResult returns:
<?xml version="1.0" encoding="UTF-8"?>
<foo:myelement xmlns:foo="http://www.mywebsite.org"
foo="http://www.mywebsite.org">content blah blah</foo:myelement>
Notice the extra attribute returned using the JDOMResult
(foo="http://www.mywebsite.org").
Is there a way to make the JDOMResult not have the extra attribute?
Thanks in advance,
Michael
Example info below
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.
Here is my empty.xml file:
========================================================================
<!-- edited with XML Spy v3.5 NT (http://www.xmlspy.com) by Maggie (OGI) -->
<root>empty</root>
========================================================================
Here is my test.xsl file:
========================================================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.mywebsite.org">
<xsl:template match="/">
<foo:myelement>content blah blah</foo:myelement>
</xsl:template>
</xsl:stylesheet>
========================================================================
Here is my test program:
========================================================================
// Imported TraX classes
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMResult;
// Imported java.io classes
import java.io.IOException;
// Imported java.net classes
import java.net.URL;
// Imported JDOM classes
import org.jdom.Document;
import org.jdom.transform.JDOMResult;
import org.jdom.output.XMLOutputter;
// Imported Serializer classes
import org.apache.xalan.serialize.Serializer;
import org.apache.xalan.serialize.SerializerFactory;
import org.apache.xalan.templates.OutputProperties;
/**
* This class tests the transformation of an XSL file into another XSL file.
*
* It compares the differences between using a JDOMResult vs. a DOMResult.
*
* To use a DOMResult: java DOM2DOMTest
* To use a JDOMResult: java DOM2DOMTest JDOM
*/
public class DOM2DOMTest
{
public static void main(String[] args)
throws TransformerException,
TransformerConfigurationException,
IOException
{
URL xslUrl = new URL("file:test.xsl");
StreamSource xslDomSource = new
StreamSource(xslUrl.openStream());
xslDomSource.setSystemId(xslUrl.getPath());
URL xmlUrl = new URL("file:empty.xml");
StreamSource xmlDomSource = new
StreamSource(xmlUrl.openStream());
xmlDomSource.setSystemId(xmlUrl.getPath());
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer =
tFactory.newTransformer(xslDomSource);
if (args.length == 1 && args[0].equalsIgnoreCase("JDOM"))
{
outputUsingJDOM(transformer, xmlDomSource);
}
else
{
useStandardOutput(transformer, xmlDomSource);
}
}
private static void useStandardOutput(Transformer transformer,
StreamSource xmlDomSource)
throws TransformerException, IOException
{
DOMResult domResult = new DOMResult();
transformer.transform(xmlDomSource, domResult);
Serializer serializer = SerializerFactory.getSerializer
(OutputProperties.getDefaultMethodProperties("xml"));
serializer.setOutputStream(System.out);
serializer.asDOMSerializer().serialize(domResult.getNode());
}
private static void outputUsingJDOM(Transformer transformer,
StreamSource xmlDomSource)
throws TransformerException, IOException
{
JDOMResult domResult = new JDOMResult();
transformer.transform(xmlDomSource, domResult);
XMLOutputter outputter = new XMLOutputter();
outputter.output(domResult.getDocument(), System.out);
}
}
========================================================================
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010606/42928f03/attachment.htm
More information about the jdom-interest
mailing list