Bugs item #2810839, was opened at 2009-06-23 13:20
Message generated for change (Comment added) made by szegedia
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=116035&aid=2810839&group_id=16035

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Attila Szegedi (szegedia)
Assigned to: Nobody/Anonymous (nobody)
Summary: Comment before document element break transform

Initial Comment:
I have a trivial document (saved as "id.xml"):

<!-- --><x/>

And a trivial XSLT file (saved as "ad.xslt") that's just an identity transform:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version='1.0'>
    <xsl:template match='@*|*|processing-instruction()|comment()' priority='-2'>
        <xsl:copy>
            <xsl:apply-templates 
select='*|@*|text()|processing-instruction()|comment()'/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

The following code, using no dom4j but just built-in javax.xml.parsers.* works, 
and prints a non-null element:

import java.io.File;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;

public class XsltTest {
    public static void main(String[] args) throws Exception {
        final Source docSource = new 
DOMSource(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new 
File("id.xml"))); 
        final DOMResult result = new DOMResult(); 
        TransformerFactory.newInstance().newTransformer(new StreamSource(
                new File("ad.xslt"))).transform(docSource, result);
        final Document doc = (Document)result.getNode();
        System.out.println(doc.getDocumentElement());
    }
}

It prints "[x: null]", as expected.

Now, replacing the "final Source docSource" declaration with a dom4j Document 
(using DOMDocumentFactory):

        final SAXReader reader = new SAXReader();
        reader.setDocumentFactory(new DOMDocumentFactory());
        final Source docSource = new DOMSource((Document)reader.read(new 
File("id.xml"))); 

will cause the program to print "null" - the transformed document now has no 
root element! 

Removing the comment from before <x/> fixes the problem, so it's the comment 
that makes it fail.

This is with 1.6.1

----------------------------------------------------------------------

>Comment By: Attila Szegedi (szegedia)
Date: 2009-06-24 09:18

Message:
For now, worked around it with a private build of dom4j. NOTE: I didn't
find and fix the bug, I worked around it. What I did is I took the
SAXReader.read(InputSource) method and doctored it so that it removes any
top-level comments from the document before returning it. Would still
prefer a proper fix.

----------------------------------------------------------------------

Comment By: Attila Szegedi (szegedia)
Date: 2009-06-23 13:22

Message:
It's possibly related to
<https://sourceforge.net/tracker/?func=detail&aid=2432779&group_id=16035&atid=116035>

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=116035&aid=2810839&group_id=16035

------------------------------------------------------------------------------
_______________________________________________
dom4j-dev mailing list
dom4j-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to