[jdom-interest] Getting comments in a Document

Edelson, Justin Justin.Edelson at mtvn.com
Thu May 11 07:31:23 PDT 2006


Are you creating the comments in your XSLT with the <xsl:comment> tag?

The XSLT spec defines the default rule for comments to be to strip them.
See http://www.w3.org/TR/xslt#built-in-rule

-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org] On Behalf Of David Patterson
Sent: Thursday, May 11, 2006 8:55 AM
To: 'Jason Hunter'
Cc: jdom-interest at jdom.org
Subject: RE: [jdom-interest] Getting comments in a Document

Jason,
Thanks for the help. I changed my code to getDescendants() and it worked
correctly. I can now see the comments.

But, the next problem surfaces. I find an Element node, create a new
Document with that node as the root, and transform it into an HTML
stream.
The transformed HTML stream does not have comments in it. There is also
no
indication from the feedback generated during the transformation that
comments are being seen.  Here is my code:

    Iterator commentIterator;
    Filter comments = new ContentFilter( ContentFilter.COMMENT );
    File inXslt = new File( xsltfn );
    File inDir = new File( sourceDir );
    File in = new File( inDir, fname );
    Document doc = JdomAssistant.docMaker( in );
    for ( commentIterator = doc.getDescendants( comments);
         commentIterator.hasNext();)
    {
        Comment comment = (Comment) commentIterator.next();
        System.out.println( "comment: " + comment.getText() );
    }
    Element root = doc.getRootElement();
    Element prelreqs = (Element) XPath.selectSingleNode( root,
           "/dmodule/content/proced/prelreqs");
    Document prelDoc = new Document( (Element) prelreqs.clone() );
    XSLTransformer tran = new XSLTransformer( inXslt );
    Document newPrelDoc = tran.transform( prelDoc );
    System.out.println( "After transform: " );
    for ( commentIterator = newPrelDoc.getDescendants( comments);
          commentIterator.hasNext();)
    {
        Comment comment = (Comment) commentIterator.next();
        System.out.println( "comment: " + comment.getText() );
    }

I see the comments in the first for loop, and none in the second. Does
the
default transformer pass comments through?

Dave Patterson 

-----Original Message-----
From: Jason Hunter [mailto:jhunter at xquery.com] 
Sent: Wednesday, May 10, 2006 6:08 PM
To: David Patterson
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Getting comments in a Document

getContent() only works one level deep.  getDescendants() might work 
better for you (it returns a lazy Iterator), or an XPath like
//comment().

-jh-


_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com



More information about the jdom-interest mailing list