FW: [jdom-interest] Trouble with Embedded HTML during XSLT Tra nsformation

Blatt, Katie KBlatt at edmunds.com
Thu Jun 14 16:26:07 PDT 2001


Per Jason's request, here is a reproducible test case for the problem
discussed below:

Jdomtest.java:

import org.jdom.*;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class Jdomtest {

    public Jdomtest() {
    }
    public static void main(String[] args) {
        Jdomtest jdt = new Jdomtest();
	jdt.createXML();
    }

    public void createXML()
    {
	try{
	    Element root= new Element("car");
	    Document doc= new Document(root);
	    Element FullButton= new Element("FullButton");
	    FullButton.addContent(new CDATA("<input type=\"hidden\"
name=\"test\">"));
	    root.addContent(FullButton);
	     Document doc2 = transform(doc, "test.xsl");
	    XMLOutputter out = new XMLOutputter("", false);
	    out.output(doc2, System.out);
	}
	catch(Exception e){System.out.println("Exception: "+e);
e.printStackTrace();}
    }

    public static Document transform(Document in, String stylesheet)
                              throws JDOMException {
    try {
      Transformer transformer = TransformerFactory.newInstance()
        .newTransformer(new StreamSource(stylesheet));
      JDOMResult out = new JDOMResult();
      transformer.transform(new JDOMSource(in), out);
      return out.getDocument();
    }
    catch (TransformerException e) {
      throw new JDOMException("XSLT Transformation failed", e);
    }
  }
}


test.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" cdata-section-elements="//styleinfo/FullButton"
omit-xml-declaration="yes"/>
<xsl:template match="/">
<form method= "get" action="test">
<xsl:value-of  select="//car/FullButton"  />
</form>
</xsl:template>
</xsl:stylesheet>


Output:
<?xml version="1.0" encoding="UTF-8"?> <form action="test"
method="get">&lt;input type="hidden" name="test"&gt;</form>


Expected/desired output:

... <input type="hidden" name="test"> ...


Katie Blatt
Software Engineer
Edmunds.com
Email: kblatt at edmunds.com 

> -----Original Message-----
> From: Jason Hunter [mailto:jhunter at collab.net]
> Sent: Thursday, June 14, 2001 2:48 PM
> To: Blatt, Katie
> Subject: Re: [jdom-interest] Trouble with Embedded HTML during XSLT
> Transformation
> 
> 
> Sounds like the transformation was causing the CDATA section 
> to be lost,
> replacing it with a non-CDATA representation of the same 
> text.  When it
> was in CDATA the special chars didn't need escaping, but on their own
> they do.  The change you made causes what should be *text* content to
> become element content on output.  That might hurt your app later.
> 
> The right solution is to look into the transformation process and see
> why the CDATA section is being lost.  Can you send in a small
> reproducible test case to the list?
> 
> -jh-
> 
> "Blatt, Katie" wrote:
> > 
> > Yes, I agree completely that it's not a good long-term 
> solution.  But for
> > now it was the only way I could get it to work for now.
> > 
> > I'm not sure how you ran your test below.  The problem I 
> was having was with
> > using the XMLOutputter *AFTER* doing an XSLT transformation (for the
> > transformation I pretty much used the sample code that is in the CVS
> > repository).  The initial Document (when I built the 
> original XML file to be
> > used for the transformation) was fine with the CDATA.  But 
> after doing the
> > transformation, I need valid HTML that will work properly 
> in a browser- but
> > instead of getting <input type=hidden"... etc. in my outputted HTML
> > document, I was getitng &lt;input type="hidden" - the 
> XMLOutputter was
> > converting the "<" to "&lt;" in the escapeElementEntities 
> method, and the
> > only way I could figure out to  get the HTML i needed was 
> to comment out the
> > code- admittedly not a pretty solution.
> > 
> > I understand the purpose of the escapeElementEntities 
> method for when you
> > are outputting pure XML.  But when what you desire after an XSLT
> > transformation is "usable" HTML, it might be nice to be 
> able to tell the
> > XMLOutputter object not to escape the "<".
> > 
> > Katie Blatt
> > Software Engineer
> > Edmunds.com
> > Ph: 310-309-6465
> > Email: kblatt at edmunds.com
> > 
> > > -----Original Message-----
> > > From: Jason Hunter [mailto:jhunter at collab.net]
> > > Sent: Tuesday, June 12, 2001 9:26 PM
> > > To: Blatt, Katie
> > > Subject: Re: [jdom-interest] Trouble with Embedded HTML 
> during XSLT
> > > Transformation
> > >
> > >
> > > I doubt that's a good long-term solution, both becaues 
> it's something
> > > that will require being done in every release, and that 
> fundamentally
> > > that escaping is proper.  In my own test I didn't see the issue.
> > >
> > > % java TestOutput cdata.xml
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <FullButton><![CDATA[<input type ="hidden" name="year"
> > > value="2001"><input
> > > type ="hidden" name="make" value="Volvo"><input type ="hidden"
> > > name="model"
> > > value="S40"><input type ="hidden" name="trim" value="SE 4dr
> > > Sedan (1.9L
> > > 4cyl
> > > Turbo 5A)">]]></FullButton>
> > >
> > > Is this different than what you saw?  I ran this against 
> the latest in
> > > CVS.
> > >
> > > -jh-
> > >
> > > "Blatt, Katie" wrote:
> > > >
> > > > Yeah- I wrote another message to the list shortly after.  I
> > > had to modify
> > > > the XMLOutputter class- I commented out these 2 blocks in the
> > > > escapeElementEntities(String st) method:
> > > >
> > > >                 case '<' :
> > > >                     stEntity = "&lt;";
> > > >                     break;
> > > >                 case '>' :
> > > >                     stEntity = "&gt;";
> > > >                     break;
> > > >
> > > > Perhaps this is something that should be configurable if
> > > you want HTML as
> > > > your output?
> > > >
> > > > Anyway, this solved my specific problem.  Thanks for asking.
> > > >
> > > > -----Original Message-----
> > > > From: Jason Hunter
> > > > To: Blatt, Katie
> > > > Sent: 6/12/01 7:37 PM
> > > > Subject: Re: [jdom-interest] Trouble with Embedded HTML 
> during XSLT
> > > > Transformation
> > > >
> > > > Did you get this solved?
> > > >
> > > > -jh-
> > > >
> > > > "Blatt, Katie" wrote:
> > > > >
> > > > > Hi all,
> > > > >
> > > > > I'm desperately seeking help with a problem I'm having
> > > with an XSLT
> > > > > transformation using JDom.  I'm pretty new to
> > > Jdom/XSLT/etc. and I'm
> > > > not
> > > > > really sure if the problem is JDOM or XALAN related.  
> Nonetheless,
> > > > here
> > > > > goes:
> > > > >
> > > > > One of my XML elements has HTML text in it. I'm storing
> > > it as CDATA:
> > > > > <FullButton><![CDATA[<input type ="hidden" name="year"
> > > > value="2001"><input
> > > > > type ="hidden" name="make" value="Volvo"><input type ="hidden"
> > > > name="model"
> > > > > value="S40"><input type ="hidden" name="trim" 
> value="SE 4dr Sedan
> > > > (1.9L 4cyl
> > > > > Turbo 5A)">]]></FullButton>
> > > > >
> > > > > After I do the transformation, the "<" and ">" are 
> all transformed
> > > > into &lt;
> > > > > and &gt;, screwing up the html formatting.  I have spent
> > > all morning
> > > > trying
> > > > > various fixes that I have found on various message
> > > boards, including:
> > > > >
> > > > > * using disable-output-esacping="yes"- this just causes
> > > my resulting
> > > > HTML
> > > > > doc to have this declartion surrounding the appropriate text:
> > > > > <?javax.xml.transform.disable-output-escaping?>
> > > > >
> > > > > * using <xsl:copy-of .../> instead of <xsl:value-of>
> > > > >
> > > > > * inserting a
> > > cdata-section-elements="//styleinfo/FullButton"  element
> > > > into
> > > > > the <xsl:output .../> declaration.
> > > > >
> > > > > I can't get anything to work.  I was searching around 
> on apache's
> > > > Xalan
> > > > > site, and it looks like Xalan *SHOULD* process this
> > > correctly.  So I'm
> > > > > wondering if it's something with JDom?  For the most
> > > part, I am using
> > > > the
> > > > > most recent Jdom .java files from the CVS (as opposed to
> > > what's in the
> > > > build
> > > > > 6 .jar file)- and I'm also using xalan.jar etc. from the Jdom
> > > > repository.
> > > > >
> > > > > Any advice is greatly appreciated!
> > > > >
> > > > > Katie
> > > > > kblatt at edmunds.com
> > > > > _______________________________________________
> > > > > To control your jdom-interest membership:
> > > > >
> > > >
> > 
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@you
> > rhost.com



More information about the jdom-interest mailing list