[jdom-interest] JDOMSource and JDOMResult can produce nothing!

Bruno Antunes bantunes at whateversoft.com
Fri Jun 22 06:59:46 PDT 2001

Hi. In fact I have missed the fact that I was transforming a JDOM Document, on another
JDOM Document, so this new transformed JDOM Document must be a valid XML document. (why
JDOM does not generate and Exception when a non valid XML transformation is performed?)

So my point now, is what is the most elegant way to transform a a JDOM Document in a
non XML file, using XSLT.

The following code works:

 * Generates the XSLT transformation for a document
 * @param doc the source JDOM Document
 * @param xslName the name of the xsl to applie
 * @param writer where the transformation will be generated
 * @throws IOException
 * @throws JDOMException
public void transform(Document doc, String xslName, Writer writer)
        throws IOException, JDOMException {
    try {
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(new StreamSource(getXSLStream(xslName)));
        XMLOutputter out = new XMLOutputter();
        /** @todo find a clever way to not generate so many strings */
        StringWriter xmlDoc = new StringWriter();
        out.output(doc, xmlDoc);
        StringReader reader = new StringReader(xmlDoc.toString());
        transformer.transform(new StreamSource(reader), new StreamResult(writer));
    } catch (TransformerException e) {
       throw new JDOMException("XSLT Transformation failed", e);

Laurent Bihanic wrote:

> You stylesheet generates ill-formed XML: The output misses a document root
> element as signaled by the java.util.EmptyStackException thrown by SAXHandler.
> Thus, JDOMResult is unable to build a JDOM document from the XLST processor
> output.
> Once I changed the first template to:
>    <xsl:template match="/">
> <root>
>     <xsl:apply-templates/>
> </root>
>    </xsl:template>
> your example runs fine.
> Please note that XMLOutputter can not output HTML, only XML, and that the
> <xsl:output ... /> tag only applies when the XSLT processor performs itself
> the output to a StreamResult.

Bruno Antunes,
Java Software Engineer

email: mailto:bruno.antunes at whateversoft.com
Phone: +351.21.7994200
Fax  : +351.21.7994242

WhatEverSoft - Java Center
Centro de Competencia Java
Praca de Alvalade, 6 - Piso 4
1700-036 Lisboa - Portugal
URL: http://www.whatevernet.com

                      INTERNET MAIL FOOTER 
A presente mensagem pode conter informação considerada confidencial.
Se o receptor desta mensagem não for o destinatário indicado, fica
expressamente proibido de copiar ou endereçar a mensagem a terceiros.
Em tal situação, o receptor deverá destruir a presente mensagem e por
gentileza informar o emissor de tal facto.
Privileged or confidential information may be contained in this
message. If you are not the addressee indicated in this message, you
may not copy or deliver this message to anyone. In such case, you
should destroy this message and kindly notify the sender by reply

More information about the jdom-interest mailing list